Java में Garbage Collection
In this page:
What is Garbage Collection?
Garbage collection JVM की heap को monitor करने और उन objects द्वारा占्रहित memory को वापस पाने की automatic process है जो अब किसी live reference से reachable नहीं हैं, developers को C या C++ की ज़रूरत की तरह manually memory deallocate करने से मुक्त करते हुए।
उदाहरण: What is Garbage Collection?
public class Main {
public static void main(String[] args) {
Object obj = new Object();
obj = null; // no longer reachable -- eligible for automatic reclamation
System.gc(); // requests garbage collection (JVM decides when it actually runs)
System.out.println("Unreachable object may be collected");
}
}
Login to try C/C++/Java/PHP code in the editor
Memory Generations
JVM heap को generations में बांटता है -- आमतौर पर नए बने, short-lived objects के लिए एक Young generation और कई collection cycles से बच चुके objects के लिए एक Old (Tenured) generation -- क्योंकि ज़्यादातर objects जल्दी मर जाते हैं, और उन्हें अलग करना collector को Young generation को तेज़ी से और अक्सर scan करने देता है।
उदाहरण: Memory Generations
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 1000; i++) {
Object shortLived = new Object(); // most objects like this die young, in the Young generation
}
System.out.println("Short-lived objects collected quickly from the Young generation");
}
}
Login to try C/C++/Java/PHP code in the editor
G1 Garbage Collector
G1 (Garbage First) collector heap को कई छोटे regions में बांटता है और सबसे ज़्यादा garbage वाले regions को पहले collect करने को प्राथमिकता देता है, जो इसे सबसे ज़्यादा फायदे वाला cleanup काम target करने देता है और बड़े heaps पर भी individual pause times को छोटा और predictable रखता है।
उदाहरण: G1 Garbage Collector
public class Main {
public static void main(String[] args) {
// Run with: java -XX:+UseG1GC Main
System.out.println("G1 divides the heap into regions, collecting the most-garbage ones first");
}
}
Login to try C/C++/Java/PHP code in the editor
Z Garbage Collector (ZGC)
Z Garbage Collector (ZGC) बहुत बड़े heaps और बेहद कम pause times के लिए डिज़ाइन किया गया है, अपना लगभग सारा काम चल रही application के साथ concurrently करके, कुछ CPU overhead की कीमत पर pauses को heap size की परवाह किए बिना single-digit milliseconds में रखते हुए।
उदाहरण: Z Garbage Collector (ZGC)
public class Main {
public static void main(String[] args) {
// Run with: java -XX:+UseZGC Main
System.out.println("ZGC targets single-digit millisecond pauses on very large heaps");
}
}
Login to try C/C++/Java/PHP code in the editor
Weak and Soft References
Weak references collector को किसी object को वापस पाने देते हैं जैसे ही कोई strong reference न बचे, भले ही एक weak reference अब भी उसे point करे -- उन caches के लिए उपयोगी जिन्हें आप garbage collection रोकना नहीं चाहते। Soft references इनसे मिलते-जुलते हैं लेकिन ज़्यादा नरम हैं: ये सिर्फ तभी clear होते हैं जब JVM में असल में memory कम पड़ रही हो।
उदाहरण: Weak and Soft References
import java.lang.ref.*;
public class Main {
public static void main(String[] args) {
Object strong = new Object();
WeakReference<Object> weak = new WeakReference<>(strong); // reclaimed as soon as no strong refs remain
SoftReference<Object> soft = new SoftReference<>(strong); // cleared only under memory pressure
System.out.println(weak.get() != null);
}
}
Login to try C/C++/Java/PHP code in the editor
- यह सोचना कि
System.gc()एक collection force करता है, जबकि यह सिर्फ एक request है जिसे JVM नज़रअंदाज़ कर सकता है। - यह मान लेना कि garbage collection सभी memory leaks रोकता है, जबकि अब भी reference किए objects, जैसे किसी static list में, कभी collect नहीं होते।
- weak और soft references को एक जैसा मानना: एक weak reference अगले collection पर clear हो जाता है, जबकि एक soft reference आमतौर पर memory कम होने तक रहता है।
Chapter Quiz — Complete all 19 topics to unlock
0/19 topics done
Complete these topics first:
- Java Reflection API
- Java Annotations Advanced
- Java Garbage Collection
- Java Memory Management
- Java Performance Optimization
- Java Advanced Interview Questions
- Java CompletableFuture
- Java Atomic Classes
- Java Locks & Semaphores
- Java Concurrent Collections
- Java Cryptography Basics
- Java Hashing (MD5, SHA)
- Java SSL & HTTPS
- Java Logging (Log4j/SLF4J)
- Java Serialization Advanced
- Java Interview Questions Advanced
- Java Connection Pooling
- Java Test Driven Development
- Java Integration Testing