1000 Java Tips ebook
Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java
programming articles and tips. Please take your copy here.
Take your copy of free "Java Technology Screensaver"!.
I am getting ''OutOfMemoryError''. How do I prevent such memory leaks?
JavaFAQ Home » General Java
Question: I am getting
"OutOfMemoryError". How do I prevent such memory leaks?
Answer: It is not always a memory leak that
A memory leak is a situation when some application, for example JVM allocates
some amount of memory and then loses pointers to that area. So, the allocated
area becomes a "black box" which you can not access and you need a key to open
It is very rare situation when some Java code causes memory leaks.
They are reported and can be found at "Bug parade" on SUN's web site.
"OutOfMemoryError" often caused by bad design. It can be for example:
1. Your program trying to process too much data. Increase the amount of memory
or decrease the amount of data to be processed.
Actually you can do it in a wise way: control how much memory left and if it
less than, let say 20 %, decrease the processing speed.
It was described in one of our tips while ago...
2. Your program holding too many objects and then program is running out of
You need to use close methods if you do need them. You can even explicitly set a
reference to null to speed up garbage collection.
3. You use too many objects with finalizers... They consume your memory before
you get any chance to run them...
Printer Friendly Page
Send to a Friend
Search here again if you need more info!