Master Java Garbage Collection: Mark‑Sweep, Copying, Compact & Generational Algorithms
This article explains the main Java garbage collection algorithms—Mark‑Sweep, Mark‑Copying, Mark‑Compact, and Generational Collection—detailing their principles, advantages, and drawbacks, helping developers choose the appropriate strategy for efficient memory management in backend applications.
In Java, common garbage collection algorithms include the following:
Mark‑Sweep Algorithm
Principle: The algorithm works in two phases: a mark phase that traverses from GC Roots (e.g., stack variables, static variables) and marks all reachable (live) objects, and a sweep phase that scans the entire heap to reclaim memory occupied by unmarked (garbage) objects.
Mark phase: Start from GC Roots, traverse and mark all referenced objects as live.
Sweep phase: Scan the whole heap and free memory of objects that were not marked.
Drawbacks:
Efficiency issue: Both marking and sweeping require a full heap traversal, which is not fast.
Space issue: The process creates many non‑contiguous memory fragments; allocating large objects may fail, causing premature GC.
Mark‑Copying Algorithm
Principle: The heap is split into two equal spaces (From and To). When the active space fills, live objects are copied to the other space, and the original space is cleared in one step.
Advantages:
High efficiency: Only live objects are copied, and clearing memory is simple and fast.
No fragmentation: The copied memory remains contiguous.
Drawbacks:
Space waste: Only half of the total heap is usable at any time; the other half holds the copied objects, leading to wasted space. This algorithm is typically used for the young generation, not the old generation.
Mark‑Compact Algorithm
Principle:
Mark phase: Same as Mark‑Sweep, mark all live objects starting from GC Roots.
Compaction phase: Move live objects toward one end of memory, eliminating gaps, then free the remaining space beyond the moved region.
Advantages:
Eliminates the fragmentation problem of Mark‑Sweep while avoiding the half‑memory waste of the copying algorithm.
Drawbacks:
The compaction process requires moving objects, which can be relatively slow, especially when many objects are present.
Generational Collection
Principle: Based on differing object lifetimes, the heap is divided into generations such as Young Generation and Old Generation.
Young Generation: Objects are typically short‑lived; the copying algorithm is usually employed for GC.
Old Generation: Objects have longer lifetimes and occupy more space; GC commonly uses Mark‑Sweep or Mark‑Compact.
Advantages: Applying different algorithms to different generations improves GC efficiency and reduces impact on application performance.
Drawbacks: Managing multiple heap regions adds complexity to the garbage collector implementation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Xuanwu Backend Tech Stack
Primarily covers fundamental Java concepts, mainstream frameworks, deep dives into underlying principles, and JVM internals.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
