JVM: garbage collector

Oak_Cassia·2024년 9월 10일
0

introduction

I didn't like garbage collection before i started working. i thought GC made programs slower. However, after getting a job and gaining more experience with coding, i realized how convenient GC actually is. Not every program needs to run at top speed, and GC helps improve productivity by allowing me to focus on more important tasks instead of manually managing memory. Nowadays, I appreciate GC because it simplifies code and improves readability. While i no longer need to release memory manually, it's still important to understand what happens when objects become unused.

How to check if objects become unused

There are two main algorithms for dertermining if objects are no longer in use: reference counting and reachability analysis.

reference counting

this method is similar to smart pointers in C++.

  • When a reference is created the counter increases by one.
  • When a reference is removed, the counter decreases by one.
  • If the counter is reaches 0, the object is consider unused.
  • Just like in C++, circular references can cause issues.

reachability analysis

  • The process starts from GC roots and traverses references, creating a reference chain.
    • GC roots are objects referenced by the JVM stack
    • or JNI, internal JVM references, objects locked for synchronization, objects reflecting the JVM state via JMXBEAN
  • Objects that do not form a reference chain are considered unused
    Objects without a reference chain are marked to determine if they need the finalize method to be called. If so, they are placed in the f-queue. After finalize is called, if they still have no refernce chain, they are collected. Objects that do not need finalization are also collected.

typse of References

  1. Strong Reference: A direct reference to an object.
  2. Soft Reference: If only soft references remain, the object is added to the list for collection right berfore memroy overflow occurs.
  3. Weak Reference: The object is collected in the next garbage collection cycle.
  4. Phantom Reference: Used for notification purposes.

In the method area, which is similar to the text area, constants and classes can be collected. String objects referncing literals are collected when no longer referenced. Classes are collected when three conditions are met: there are no instances of the class, the class loader has been collected, and there are no references to the class(including java.lang.Class refereces or reflection).

generation

  • Every object dies early
  • The objects that survive tend to live longer

Then, we separate them into new and old objects. New objects are frequently collected, while old objects are only collected when necessary. GC needs to check for references from old objects to new ones, even when it intends to collect only the new generation. However, since cross-generational references between old and new generations are rare, it's inefficient to scan the entire old generation or record every reference. Instead, it typically checks for references from the old generation to specific memory sets.

basic algorithm for garbage collection

  1. Mark-Sweep
    Mark objects that should either be collected or survive, then collect the rest. If the heap contains many objects, this approach can be inefficient, and it may lead to significant memory fragmentation.

  2. Mark-Copy
    Separate memory into two blocks. Record objects in one block. After collection, copy surviving objects to the other block.
    The new generation garbage collection uses this algorithm. For efficiency, memory is divided into one large block and two smaller blocks. During memory allocation, only the large block and one small block are used. After collection, survivors are copied to the other small block. However, if the surviving objects do not fit into the available space, they are directly added to the old generation.

  3. Mark-Compact
    Similar to Mark-Sweep, objects are marked for collection. However, after marking, surviving objects are compacted to one side of memory.
    The old-generation garbage collection uses this algorithm. The compaction process is expensive, so it isn't done every time. Instead, it is deferred until memory fragmentation becomes severe enough to impact memory allocation.

profile
어떻게 살아야 하나

0개의 댓글