Master Java Garbage Collection: Algorithms, Parameters, and Interview Tips

This article explains Java's garbage collection fundamentals, covering JVM memory layout, key heap parameters, object liveness determination, and the main GC algorithms—Mark‑Sweep, Copying, and Mark‑Compact—while providing practical insights for interview preparation.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
Master Java Garbage Collection: Algorithms, Parameters, and Interview Tips

JVM Memory Layout Recap

The Java heap is divided into three regions: Eden, From Survivor, and To Survivor, with a typical size ratio of 8:1:1. This design reflects the behavior of objects during garbage collection.

JVM Heap Parameters

-Xms sets the initial heap size.

-Xmx sets the maximum heap size.

-Xmn defines the initial and maximum size of the young generation (refined by NewSize and MaxNewSize).

-XX:NewSize sets the minimum young generation size.

-XX:MaxNewSize sets the maximum young generation size.

-XX:PermSize sets the minimum permanent generation size.

-XX:MaxPermSize sets the maximum permanent generation size.

-Xss sets the stack size for each thread.

From these parameters you can compute the old generation size:

老年代空间大小=堆空间大小-年轻代大空间大小

Mnemonic for remembering the main parameters: Xmx (memory maximum), Xms (memory startup), Xmn (memory nursery/new), Xss (stack size).

Parameter prefixes meaning: - standard VM option, -X non‑standard VM option (not guaranteed on all VMs), -XX advanced/unstable option.

GC Overview

Garbage Collection (GC) is performed automatically by the JVM, but developers must understand and tune GC parameters to avoid memory leaks, out‑of‑memory errors, and performance bottlenecks.

How Objects Are Determined Live

Two main approaches are used:

Reference counting : each object has a counter incremented on reference acquisition and decremented on release; objects with a count of zero are reclaimed. This method is simple but cannot handle cyclic references, so it is not used in mainstream JVMs.

Reachability analysis : starting from GC Roots (system class loaders, thread stacks, active threads, etc.), the JVM traverses object references. Objects not reachable from any root are considered garbage.

Mark‑Sweep Algorithm

The Mark‑Sweep algorithm consists of a marking phase that identifies all reachable objects, followed by a sweeping phase that reclaims the memory of unmarked objects. While conceptually simple, it suffers from low efficiency and can create fragmented memory, leading to additional GC cycles.

Copying Algorithm

The Copying algorithm splits the heap into two equal halves. During a collection, live objects are copied from the active half to the other half, and the former half is cleared. This eliminates fragmentation and makes allocation fast (just bump the heap pointer), but it effectively halves usable memory and can be inefficient when many objects survive.

Mark‑Compact Algorithm

Mark‑Compact combines marking with a compaction step: after marking live objects, they are moved together to one end of the heap, and the remaining space is reclaimed. This avoids fragmentation without the 50% memory overhead of copying.

Generational Collection

The heap is logically split into the young generation and the old generation. Young‑generation objects are short‑lived and are collected using the Copying algorithm. Objects that survive multiple collections are promoted to the old generation, where the Mark‑Compact algorithm is used due to higher survival rates.

Conclusion

Understanding these GC concepts and parameters equips you to answer interview questions confidently and to tune Java applications for better performance.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

interviewAlgorithmsgcgarbage-collectionmemory-management
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.