Saturday, July 15, 2017

Java Garbage Collection

HotSpot Memory Structure

Weak generational hypothesis
  1. most objects die young
  2. reference from old objects to young is very small.( example : map )
When does GC happen ?
When Eden Space is filled up. During GC referenced objects are moved to survivor space and un referenced are kept in memory.

Object Aging : 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). In addition, objects from the last minor GC on the first survivor space (S0) have their age incremented and get moved to S1. Once all surviving objects have been moved to S1, both S0 and eden are cleared. Notice we now have differently aged object in the survivor space. (thats why two different Survivor Spaces)
Promotion : when the GC count for certain objects increase from threshold ( here 8 ) its promoted
Concurrent Mark and Sweep

  1. Initial Mark: The application threads are paused in order to collect their object references. When this is finished, the application threads are started again.
  2. Concurrent Mark: Starting from the object references collected in phase 1, all other referenced objects are traversed.
  3. Concurrent Preclean: Changes to object references made by the application threads while phase 2 was running are used to update the results from phase 2.
  4. Remark: As phase 3 is concurrent as well, further changes to object references may have happened. Therefore, the application threads are stopped once more to take any such updates into account and ensure a correct view of referenced objects before the actual cleaning takes place. This step is essential because it must be avoided to collect any objects that are still referenced.
  5. Concurrent Sweep: All objects that are not referenced anymore get removed from the heap.
    Concurrent Reset: The collector does some housekeeping work so that there is a clean state when the next GC cycle starts.

Types of GC
Left Side : Concurrent : Application code is running parallel to GC.
Right Side : Serial

Figure 4: Difference between the Serial GC and Parallel GC.

Ques : Are young GC always stop world ?
For OpenJDK, JRockit, IBM JVM, and Sun/Oracle JDK, the young collection is always stop the world for every available collector. (Sep 2013) only one paid JVM has non stop world GC.

Ques : What happens when the To Space is filled ?
If the To space becomes full, the live objects from Eden or From that have not been copied to it are tenured, regardless of how many young generation collections they have survived. Any objects remaining in Eden or the From space after live objects have been copied are, by definition, not live, and they do not need to be examined.

Young generation sizing is important...
  1. If the young generation is too small, short-lived objects will quickly be moved into the old generation where they are harder to collect. Conversely,
  2. if the young generation is too large, we will have lots of unnecessary copying for long-lived objects that will later be moved to the old generation anyway.

What Causes Objects to Move from Young Generation to Old Generation ?
Ans : Either of Young or Old GC , is responsible for object to move from young gen to old gen

  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.

No comments: