|
| 1 | +Garbage Collection (GC) in Java |
| 2 | + |
| 3 | +1. Introduction: |
| 4 | +- Garbage Collection (GC) in Java is an **automatic memory management process**. |
| 5 | +- It identifies and removes **unused objects** from the heap memory to free up space. |
| 6 | +- This helps prevent **memory leaks** and ensures efficient use of memory. |
| 7 | +- Unlike C/C++, where the programmer manually frees memory, Java’s GC runs automatically. |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +2. What is Garbage?: |
| 12 | + |
| 13 | +- Any object in memory that is **no longer reachable** by any live thread or reference. |
| 14 | +- Example: |
| 15 | + |
| 16 | + Person p = new Person("Alice"); |
| 17 | + p = new Person("Bob"); |
| 18 | + |
| 19 | + - First `Person("Alice")` object is now unreachable (no reference points to it). |
| 20 | + - GC will clean it up. |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | +3. How Does GC Work?: |
| 25 | + |
| 26 | +GC works in three main steps: |
| 27 | + |
| 28 | +a) **Mark Phase** |
| 29 | +- GC identifies all objects that are still reachable (marked as alive). |
| 30 | +- Starting from **GC Roots** (local variables, active threads, static fields, etc.), |
| 31 | + it traverses the reference graph. |
| 32 | + |
| 33 | +b) **Sweep Phase** |
| 34 | +- All unmarked objects (unreachable ones) are considered garbage. |
| 35 | +- Memory occupied by these objects is marked as free. |
| 36 | + |
| 37 | +c) **Compact Phase (Optional)** |
| 38 | +- To reduce fragmentation, live objects are moved together in memory. |
| 39 | +- This creates a contiguous free space, improving allocation efficiency. |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +4. ASCII Diagram of GC Process: |
| 44 | + |
| 45 | +Before GC (Heap Memory): |
| 46 | + |
| 47 | +[Obj1: Alive] [Obj2: Unreachable] [Obj3: Alive] [Obj4: Unreachable] |
| 48 | + |
| 49 | +Mark Phase: |
| 50 | + |
| 51 | +Obj1 (reachable) |
| 52 | +Obj3 (reachable) |
| 53 | +Obj2, Obj4 (not reachable → garbage) |
| 54 | + |
| 55 | +Sweep Phase: |
| 56 | + |
| 57 | +[Obj1: Alive] [Free] [Obj3: Alive] [Free] |
| 58 | + |
| 59 | +Compact Phase: |
| 60 | + |
| 61 | +[Obj1: Alive] [Obj3: Alive] [Free] [Free] |
| 62 | + |
| 63 | +--- |
| 64 | + |
| 65 | +5. Types of Garbage Collectors: |
| 66 | + |
| 67 | +Java provides multiple GC implementations: |
| 68 | + |
| 69 | +1. **Serial GC** |
| 70 | +- Single-threaded, simple, best for small applications. |
| 71 | + |
| 72 | +2. **Parallel GC** |
| 73 | +- Uses multiple threads for faster GC, suitable for medium-scale apps. |
| 74 | + |
| 75 | +3. **CMS (Concurrent Mark-Sweep)** |
| 76 | +- Reduces GC pause times by running concurrently with the application. |
| 77 | + |
| 78 | +4. **G1 (Garbage First) GC** |
| 79 | +- Default in modern Java (since Java 9). |
| 80 | +- Splits heap into regions and collects garbage in parallel, focusing on regions with the most garbage first. |
| 81 | + |
| 82 | +--- |
| 83 | + |
| 84 | +6. Finalization and GC |
| 85 | +---------------------- |
| 86 | +- Before an object is destroyed, `finalize()` method (if overridden) may be called once. |
| 87 | +- But using `finalize()` is discouraged due to unpredictability. |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +7. Important Points |
| 92 | +------------------- |
| 93 | +- GC runs **automatically**, but you can suggest it using `System.gc()`. |
| 94 | + (Not guaranteed, JVM decides when to run.) |
| 95 | +- GC focuses only on **heap memory** (not stack memory). |
| 96 | +- GC prevents **memory leaks** by cleaning unused objects. |
| 97 | +- Programmers still need to avoid creating unnecessary references. |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +8. Real-Life Analogy |
| 102 | +--------------------- |
| 103 | +Think of GC as a **housekeeper**: |
| 104 | +- Mark: Identify which items in the room are still useful. |
| 105 | +- Sweep: Throw away items that nobody uses anymore. |
| 106 | +- Compact: Rearrange the remaining items neatly to make space. |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +✔ Summary |
| 111 | +--------- |
| 112 | +- Garbage Collection = Automatic memory cleanup of unreachable objects. |
| 113 | +- Works in **Mark → Sweep → Compact** phases. |
| 114 | +- Multiple GC algorithms exist (Serial, Parallel, CMS, G1). |
| 115 | +- Helps prevent memory leaks and improves memory efficiency. |
| 116 | + |
| 117 | +⸻ |
| 118 | + |
| 119 | +✔ Strong reference: object tab tak alive rahega jab tak koi strong reference use point kare. |
| 120 | +✔ WeakReference: object weakly reachable hai; GC kabhi bhi clear kar sakta hai jab koi strong reference na ho. |
| 121 | +✔ ReferenceQueue: jab weak/phantom references clear hoti hain, |
| 122 | +JVM unko is queue me dal deta hai — isse app ko pata chal sakta hai ki object reclaim hua. |
| 123 | +✔ System.gc() sirf hint hai — JVM decide karta hai kab GC chalana hai. |
0 commit comments