JVM memory is divided into two categories: young generation space and old generation space. As the names imply, young generation space is meant for recently created objects and old generation space stores surviving objects that have lived to some extent. Young generation space is itself divided into three categories: Eden space and two survivor spaces. The JVM initially allocates objects in the Eden space, where most objects die and quickly are reclaimed. When Eden space fills up, it causes the JVM to perform a minor collection, when it moves some surviving objects to the old generation. (Note: Any new objects created inside the method go into Eden space and the objects space is reclaimed once the method exists. Class-level object variables hang around for the entire life of the objects.
The two survivor spaces are for copying live objects, allowing young objects to remain in the young generation space longer. One survivor space is empty at any given time. It serves as the destination of the next copying collection of any living objects in the Eden space and the other survivor space. In Figure 1, survivor space S1 is empty while S0 is being used.
|Figure 1. Jstat Snapshot of a Running Application|
If objects get too old, or young generation space gets filled up, JVM promotes objects to the old generation space. When the old generation space gets filled up, the JVM performs a major collection to remove the unused objects and reclaim their space. A major GC collection takes a significant amount of time and can affect system performance. Therefore, developers must make sure that major collections do not happen too often in their applications.
Explicitly nulling the object variables reduces the burden on the JVM, which can make a difference. An alternative, using
System.gc(), is not a good programming practice because it forces the System to recapture the old generation space, which can take a long time.