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.
Capture.PNG

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)
Capture.PNG
Promotion : when the GC count for certain objects increase from threshold ( here 8 ) its promoted
Capture.PNG
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.













No comments: