ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Garbage Collection (GC)
    StaticPL/JAVA 2019. 8. 23. 06:51

    1. Overview

    Since Java oriented programs don't deallocate memory explicitly, Garbage collection tracks each and every object available in the JVM space and removes unused ones. GC works in two simple steps known as Mark and Sweep.

    2. Minor GC

    Collecting garbage from Young space (consisting of Eden and Survivor spaces) is called a Minor GC. This definition is both clear and uniformly understood. But there are still some interesting takeaways you should be aware of when dealing with Minor Garbage Collection events:

    • Minor GC is always triggered when JVM is unable to allocate space for a new Object, e.g. the Eden is getting full. So the higher the allocation rate, the more frequently Minor GC is executed.
    • Whenever the pool is filled, its entire content is copied and the pointer can start tracking the free memory from zero again. So instead of classical Mark, Sweep and Compact, cleaning Eden and Survivor spaces is carried out with Mark and Copy instead. So, no fragmentation actually takes place inside Eden or Survivor spaces. The write pointer is always residing on the top of the used pool.
    • During a Minor GC event, Tenured generation is effectively ignored. References from tenured generation to young generation are considered de facto GC roots. References from young generation to Tenured generation are simply ignored during the markup phase.
    • Against common belief, all Minor GCs do trigger stop-the-world pauses, stopping the application threads. For most of the applications, the length of the pauses is negligible latency-wise. This is true if most of the objects in Eden can be considered garbage and are never copied to Survivor/Old spaces. If the opposite is true and most of the newborn objects are not eligible for GC, Minor GC pauses start taking considerably more time.
    • Objects older than 15 GC cycles, Go to Tenured(Old) generation area where major GC is occured.

    So with Minor GC, the situation was rather clear – every Minor GC cleans the Young generation.

    3. Major GC

    Major GC is cleaning the Tenured(Old) space.

    Unfortunately, it is a bit more complex and confusing. To start with – many Major GCs are triggered by Minor GCs, so separating the two is impossible in many cases. On the other hand – many modern garbage collections perform cleaning the Tenured space partially, so again, using the term “cleaning” is only partially correct.

    This leads us to the point where instead of worrying whether the GC is called Major or Full GC, you should focus to finding out whether the GC at hand stopped all the application threads or was it able to progress concurrently with the application threads.

    4. Full GC

    Full GC is cleaning the entire Heap – both Young and Tenured(old) spaces.

    5. Description

    5.1 Component of Heap

    hypotheses based on GC called the weak generational hypothesis. 

    • Most objects soon become unreachable
    • References from old objects to young objects only exist in small numbers

    In order to preserve the strengths of this hypothesis, it is physically divided into two - young generation and old generation in HotSpot VM.

    5.1.1 Young generation

    Most of the newly created objects are located here. almost objects become unreachable soonly, many objects are created in the young generation, then deallocated which is called minor GC.

    5.1.2 Old generation

    The objects that did not become unreachable and survived from the young generation are moved here. It is generally larger than the young generation and GC called major GC occurs less frequently than in the young generation.

    5.1.3 Permanent Generation

    Also known as method area. It stores classes or interned character strings. A GC may occur in the area and counted as a major GC.

    5.1.4 Card table

    To handle objects in the old generation need to reference an object in the young generation, card table stands which is a 512-byte chunk.  Whenever an object in the old generation references an object in the young generation, it is recorded in the table. And if minor GC will occur, only this card table is searched to determine whether or not it is subject for GC, instead of checking the reference of all the objects in the old generation. this card is managed with write barrier which is a device that allows a faster performance for minor GC with a bit of overhead producing reducing the overall GC time.

     

     

    5.2 Composition of the Young Generation

    5.2.1 One Eden space

    • newly created objects are located in the Eden space.
    • After one GC in Eden space, the surviving objects are moved to one of the Survivor spaces

    5.2.2 Two Survivor Spaces

    • Once a Survivor space is full, surviving objects are moved to the other Survivor space. Then, the Survivor space that is full will be changed to an empty state.
    • The objects have been repeated a number of times are moved to the old generation.
    • One of the Survivor spaces must remain empty. If not, some things are wrong in your system.

     

    Two techniques are used for faster memory allocations in HotSpot VM. One is called bump-the-pointer, and the other is called TLABs(Thread-Local Allocation Buffers)

    • Bump-the-pointer technique tracks the last object allocated to the Eden space. But if the multithreaded environment, an inevitable lock will occur and the performance will drop due to the lock-contention.
    • TLABs is the solution to this problem in HotStop VM. This allows each thread to have a small portion of its Eden space that corresponds to its own share. 

    5.3 GC for the Old Generation (Major GC)

    Major GC occurs when the data is full. The execution procedure varies by the GC type under below. GC Implementations in JDK 7

     

    5.3.1 Serial Garbage Collector

    • -XX:+UseSerialGC
    • This type drops the application performance significantly and only created when one CPU core on desktop computers.
    • mark-sweep-compact
      • To mark the surviving objects in the old generation(Mark)
      • Check heap from the front and leaves only the surviving ones behind(Sweep)
      • Fill up the heap from the front with the objects consecutively, and eventually divide the heap into two parts: one with objects and one without objects(Compact)
      • Suitable for small memory and number of CPU cores.

    5.3.2 Parallel Garbage Collector

    • Parallel GC (-XX:+UseParallelGC)
      • Use several threads to process a GC, and therefore, faster. 
      • Suitable for Enough memory and a larget number of cores
      • Called throughput GC
    • Parallel Old GC(-XX:+UseParallelOldGC)
      • Being supported since JDK 5. The only difference with Parallel Garbage Collector is this is for the old generation. it goes through 3 steps: mark - summary - compaction. summary step a little more complicated than sweep step because the summary step identifies the surviving objects separately for the areas that the GC have previously performed.

    5.3.3 Concurrent Mark Sweep (CMS) Garbage Collector

    • -XX:+UseConcMarkSweepGC
    • Called low latency GC
    • Used when the response time from all applications is crucial
    • Initial mark step is simple
      • The surviving objects among the objects the closest to the classloader are searched which is very short pausing time. 
    • Concurrent Mark Step
      • The objects referenced by the surviving objects that have just been confirmed are tracked and checked
      • It proceeds while other threads are processed at the same time.
    • Remark Step
      • The objects that were newly added or stopped being referenced in the concurrent mark step are checked
    • Concurrent Sweep Step
      • GC procedure takes place.
      • Other threads are still being processed.
      • The pausing time for GC is very short in this manner.

     

    5.3.4 G1 Garbage Collector

    • Garbage first(G1) GC
    • Fastest GC than any other GC types above all.
    • No young generation and old generation
    • One Object is allocated to each grid, and then a GC is executed.
    • Once one area is full, the objects are allocated to another area, and then a GC is executed.

     

    5. References

    https://www.geeksforgeeks.org/mark-and-sweep-garbage-collection-algorithm/

    https://plumbr.io/blog/garbage-collection/minor-gc-vs-major-gc-vs-full-gc

    https://www.cubrid.org/blog/understanding-java-garbage-collection

    https://www.baeldung.com/jvm-garbage-collectors

    'StaticPL > JAVA' 카테고리의 다른 글

    Checked and Unchecked Exceptions  (0) 2019.08.23
    Fork Join framework  (0) 2019.08.23
    JDK, JRI, JVM, and Classloader  (0) 2019.08.23
    Callable  (0) 2019.08.21
    Executors Framework and Thread Pools  (0) 2019.08.21

    댓글

Designed by Tistory.