brief introduction to java generation memory

Java VM uses a generational garbage collector, which relies on the below two observations:

  • Most allocated objects will become unreachable quickly
  • There are few references from older object to younger objects

According to the above hypothesis, Java VM divides the heap into three generations.

+------------------+----------------+----------------------+
| Young Generation | Old Generation | Permanent Generation |
+------------------+----------------+----------------------+

Permanent Generation is only used by Java VM to hold metadata, such as class structures, internal strings and so on. Its usage is little affected by user-allocated objects, so I will not cover it too much. Most of the time, a running application will interact with the objects in the Young Generation and Old Generation.

Young Generation can be further divided into the below three spaces:

+------+---------------------+---------------------+
| Eden | Survivor Space (S0) | Survivor Space (S1) |
+------+---------------------+---------------------+

When an application allocates a new object using the new keyword, the object will be created in the Eden space. When the application runs for enough time, the newly allocated objects will nearly take most of the Eden space. The garbage collector will collect those unused objects in the Eden space and move the in-use objects into one of the survivor spaces, for example, S0. This procedure will leave the Eden space empty.

When the application runs for another period of time, the newly allocated object will again nearly take most of the Eden space. The garbage collector will collect those unused object in the Eden space and move the in-use objects into the other survivor space, S1. Beside this, the garbage collector will also collect those unused objects in the survivor space S0 and move those in-use objects in the other survivor space S1. This step repeats again and again during the application running.

The objects that are living for a long time will be shifted between the two survivor spaces enough times. When a threshold reaches, these objects that are not garbage collected will be moved to the Old Generation.

During the application running, the garbage collector will also collect those unused objects and retain those in-use objects in the Old Generation. Those in-use objects will not be moved to other spaces or generations. However, different garbage collection algorithms handles those retained objects differently. Some algorithm may leave those objects where they are, while some algorithm may move those objects to the beginning of the Old Generation, which in fact compacting those objects.

The garbage collection taking place in the Young Generation is usually referred as minor garbage collection, while the garbage collection taking place in the Old Generation is usually referred to major garbage collection. Minor garbage collection usually takes place more frequently than major garbage collection, and takes less time than major garbage collection.