Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What cause Objects to move from Young Generation to Old Generation

After many cycles of GC, Objects that are survived in young generation are moved to the Old generation memory space.

Please clarify, Minor GC is responsible for this ? or Major GC?

like image 223
Rohit Shekhar Avatar asked Oct 08 '16 13:10

Rohit Shekhar


People also ask

What is young generation and old generation?

Hence we can say that the young gen contains the short lived objects while the old gen contains the objects having a long life. The permanent gen does not interact with the other two generations. Follow this answer to receive notifications.

On what basis will you decide the young generation and old generation size for an application?

The optimal choice depends on the lifetime distribution of the objects allocated by the application. By default, the young generation size is controlled by the parameter NewRatio . For example, setting -XX:NewRatio=3 means that the ratio between the young and tenured generation is 1:3.

What is perm gen and other generation for objects?

Permanent Generation or “Perm Gen” contains the application metadata required by the JVM to describe the classes and methods used in the application. Note that Perm Gen is not part of Java Heap memory. Perm Gen is populated by JVM at runtime based on the classes used by the application.

Which generation contains objects that have reached the minor GC age threshold?

The young generation is divided into three regions: an Eden space and two survivor spaces. Most objects (very large objects will be the exception) are allocated in Eden space using a simple (and fast) pointer bumping method. When the pointer reaches the end of the Eden space, a minor GC needs to occur.


2 Answers

Please clarify, Minor GC is responsible for this ? or Major GC?

Either of them is responsible for object to move from young gen to old gen.

Have a look at "General Garbage Collection Process" section @ oracle garbage collection tutorial

enter image description here

Summary:

  1. First, any new objects are allocated to the eden space. Both survivor spaces start out empty.

  2. When the eden space fills up, a minor garbage collection is triggered

  3. Referenced objects are moved to the first survivor space. Unreferenced objects are deleted when the eden space is cleared.

  4. At the next minor GC, the same thing happens for the eden space. Unreferenced objects are deleted and referenced objects are moved to a survivor space. However, in this case, they are moved to the second survivor space (S1)

  5. At the next minor GC, the same process repeats. However this time the survivor spaces switch. Referenced objects are moved to S0. Surviving objects are aged. Eden and S1 are cleared.

  6. After a minor GC, when aged objects reach a certain age threshold (8 in this example) they are promoted from young generation to old generation.

  7. As minor GCs continue to occur, objects will continue to be promoted to the old generation space.

  8. Eventually, a major GC will be performed on the old generation which cleans up and compacts that space.

like image 125
Ravindra babu Avatar answered Oct 08 '22 16:10

Ravindra babu


Objects can be moved from young to tenured space in either a Minor GC (young space) or Full GC (everything). A Major GC collection only collects in the tenured space.

Smaller objects are created in the Eden space, Large obejcts e.g. arrays in the tenured space.

When the Eden space is cleared out, surviving objects are copied to the survivors spaces. They are copied back and forth between the two survivors spaces until their age (number of times copied) reaches the tenuring threshold and in which case it is copied to the tenured space.

If there is too many objects in the Eden space to be copied to the survivor spaces, a full GC is triggered and all live object go straight to the tenured space.

Considering the size (survivorRatio) survivor size will always be greater than eden

The survivor ratio is how much smaller the survivor space is than the Eden space. e.g. -XX:SurvivorRatio=8 means the survivors space is 1/10th of young generation. There is two survivors spaces (1/10th each) and the Eden space is 8 times larger (8/10ths)

Even if the survivors space was larger than the Eden, it's the amount of free space in the survivor which matters. You could have a survivor space which is 90% full for example (as it still has objects from the last N collections)

So will there be ever a scenario to have that objects directly gets copied from eden to Old if objects are smaller in size (not humongous) ?

If you make the survivor spaces small enough to trigger a full collection each time, the objects will go from Eden to Tenured. I don't recommend going this.

like image 22
Peter Lawrey Avatar answered Oct 08 '22 15:10

Peter Lawrey