When invoking System.gc()
in java (via JMX), it will dutifully (attempt to) clean the young generation. This generally works very well. I have never seen it attempt to clean the tenured generation, though. This leads me to two questions:
- Can the tenured generation even be collected (i.e. is there actually garbage in this generation, or do all objects in the tenured generation actually still have live references to them)?
- If the tenured generation can be collected, can this be done via System.gc(), or is there another way to do it (unlikely), or will I simply have to wait until I run out of space in the tenured generation?
I beg to differ. The Java 6 GC tuning guide actually says this:
Note the use of the word "can" rather than "will". My reading of this sentence is that it does not state that a major collection will be done. It might be done, or it might not. The point that I think the authors are really trying to make here (and elsewhere) is that calling
System.gc()
may cause the collector to do a lot of unnecessary work.Now it may be that calling
System.gc()
does cause a major collection each time ... for a certain versions of the JVM. But you should not rely on this being the case for all versions, especially future ones.Exceptional article on Java's garbage collection here: Tuning Garbage Collection with the Java 5 VM It is specifically for Java 5, but most of it probably still applies to later VMs.
Simple thumb rule for garbage collection in java is,
Collections
class from java utils for collection manipulations.Learn techniques specific to frameworks. Say in hibernate, do not use
ArrayList
for one-to-many relationships, because Lists are ordered so it will make extra column for children's ordering. UseSet
instead.Do not use Hsql. Use some sort of relational database like postgres etc... HSQL will eat more memory. I have faced issues regarding this.
Also keep in mind that when you using XML handling, do not use DOM when you just want to read small amount of data form XML. DOM will make whole structure of XML in memory, so will take more memory.
Try not to keep objects in memory which will grow with time. Otherwise the applications can be out of memory.
define sizes for your lists, maps etc...
Just do not use any framework for small needs. If so then carefully check how you can tweak configurations for better and smaller heap area.
Just calling
System.gc()
doen't free your memory and cleaning up objects.Guys, please add more details on this if i missed. So Others can check it out for better performance. thanks.
http://java.sun.com/javase/technologies/hotspot/gc/gc_tuning_6.html#other_considerations states that System.gc() triggers a
major
collection, i.e. including tenured.Have you seen this not to be the case in the GC logs?