- Objects when created are always first allocated to Eden.
- When Eden fills up, a fast but not comprehensive GC (minor collection) is run over the young generation only.
- All surviving objects are moved from Eden into one Survivor Space.
- In consequent minor collections, new objects move from Eden into the other Survivor Space, plus everything from the first Survivor Space (survivors from the previous minor collection) is also moved into the second Survivor Space. Thus one survivor should be empty at that time.
- When objects in Survivor Space are old enough (or survivor fills up), they are moved to Tenured. By default the long-lived objects may be copied up to 31 times between the Survivor Spaces before they are finally promoted to the Old generation.
- When tenured fills up, a Full GC collection is run that is comprehensive: the entire heap is analyzed, all objects that can be destroyed are killed and memory is reclaimed.
- Note: the above lifecycle changes slightly when advanced options such as ConcurrentMarkSweep etc are enabled.
Wednesday, December 9, 2009
Java Object Life Cycle and Garbage Collection
Java object life cycle and garbage collection occurs like this:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment