I guess managing the Java heap goes under Performance (Should this forum be called Optimization instead?) anyways.. Could anyone list coding techniques that programmers should use and avoid when it comes to managing the Java heap. More specifically I want to know the coding techniques to avoid that would eventually lead to an OutofMemoryError.
A couple off the top of my head are: - Make sure you always clean up non-memory resources. Things like file and socket handles. These are not cleared by the GC. All the GC handles in object memory. - Don't clean up these resources in a finalize() method. They are not guaranteed to get called. A good place to code these is in finally blocks or somewhere where you know they will get called and release. - Don't hang on to references to objects that you don't need. Sometimes this happens when a class is loaded that holds a reference to a large object, or many objects. Consider the case where, at some point, you need this class, but not everything it references. Since it is referencing these objects that you don't need it is wasting memory. You can specifically null these references in hopes that the GC will clean them up on a future run. - You can also use command line options to increase the size of the heap that the VM uses. Again, just a quick list, I'm sure others have some ideas too. Peter Haggar ------------------ Senior Software Engineer, IBM author of: Practical Java