views:

106

answers:

4

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:

  1. 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)?
  2. 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?
+3  A: 

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.

Rulmeq
The article, indeed, does specify that the tenured generation is collected. I found that article before, but apparently missed that piece of information. Thanks.
Markus Jevring
+4  A: 

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?

JoseK
Nope. Perhaps I've just been unlucky and/or sloppy. Thanks.
Markus Jevring
+2  A: 

I beg to differ. The Java 6 GC tuning guide actually says this:

This can force a major collection to be done when it may not be necessary (i.e., when a minor collection would suffice), and so in general should be avoided.

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.

Stephen C
I'm aware that calling System.gc() should be considered a recommendation only. What I was after was "if the collection actually happens, is the tenured generation a possible target". This seems to be the case.
Markus Jevring
@Markus - fair enough. My point was really to correct misstatements about what the GC tuning guide says.
Stephen C
A: 

Simple thumb rule for garbage collection in java is,

  • Use memory cleanup techniques like cleaning up resources, members and instances when not needed.
  • Close what you open. (W.R.T. connection, hibernate session etc...)
  • Use buffering techniques while using file IO.
  • Use new NIO instead of older File IO.
  • Use Collections class from java utils for collection manipulations.
  • Use Array when possible.
  • 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. Use Set 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.

Paarth
Thank you. I'm aware of how software development should be done. True as your points are, they are off topic.
Markus Jevring
thats ok, i was just wondering to help :).
Paarth