Java में Memory Management
In this page:
Stack vs Heap Memory
Java local variables और method call frames को हर thread के अपने Stack पर allocate करता है, जो तेज़ है और किसी method के return होने पर अपने आप साफ़ हो जाता है, जबकि असली object instances shared Heap में रहते हैं, जिसे इसके बजाय garbage collector manage करता है।
उदाहरण: Stack vs Heap Memory
public class Main {
static class Point { int x; } // instances live on the Heap
public static void main(String[] args) {
int localVar = 5; // lives on the Stack, cleaned up when main() returns
Point p = new Point(); // the reference is on the Stack, the object itself is on the Heap
System.out.println(localVar + " " + p);
}
}
Login to try C/C++/Java/PHP code in the editor
Memory Leak Identification
Java में memory leaks तब होते हैं जब code उन objects के references रखता है जिनकी उसे असल में अब ज़रूरत नहीं -- बढ़ती static list, या बिना बंद हुआ listener registration -- जो garbage collector को उन objects को वापस पाने से रोकता है भले ही program उनका फिर कभी उपयोग न करे।
उदाहरण: Memory Leak Identification
import java.util.*;
public class Main {
static List<Object> cache = new ArrayList<>(); // static list -- keeps growing, never cleared
public static void main(String[] args) {
for (int i = 0; i < 5; i++) cache.add(new Object());
System.out.println("Cache size: " + cache.size() + " -- these can never be garbage collected");
}
}
Login to try C/C++/Java/PHP code in the editor
OutOfMemoryError (OOM)
JVM एक OutOfMemoryError फेंकता है जब heap allocation requests इसके लिए असल में उपलब्ध memory से ज़्यादा हों, जो आमतौर पर या तो एक असली memory leak या application के असली workload के लिए बहुत छोटी configured heap size limit (-Xmx) को संकेत करता है।
उदाहरण: OutOfMemoryError (OOM)
public class Main {
public static void main(String[] args) {
try {
int[] huge = new int[Integer.MAX_VALUE]; // exceeds available heap
} catch (OutOfMemoryError e) {
System.out.println("Caught OutOfMemoryError: heap exceeded");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Metaspace Configuration
Class metadata -- loaded classes की compiled structure खुद, object instances नहीं -- Metaspace में रहता है, एक dynamically-sized memory region जो regular Java heap के बजाय native (off-heap) system memory से लेता है।
उदाहरण: Metaspace Configuration
public class Main {
public static void main(String[] args) {
// Run with: java -XX:MaxMetaspaceSize=128m Main
System.out.println("Class metadata lives in Metaspace, drawn from native memory, not the heap");
}
}
Login to try C/C++/Java/PHP code in the editor
Finalization and Cleaner API
Cleaner API किसी object के lifecycle से बंधे off-heap resources (जैसे native memory या file handles) को release करने का एक ज़्यादा सुरक्षित, predictable तरीका प्रदान करता है, उस पुराने, अविश्वसनीय Object.finalize() mechanism को replace करते हुए जिसकी JVM ने कभी जल्दी चलने की गारंटी नहीं दी, अगर बिल्कुल भी चले।
उदाहरण: Finalization and Cleaner API
// Import java.lang.ref.Cleaner so it can be used by its short name
import java.lang.ref.Cleaner;
// Define the class Main
public class Main {
static final Cleaner cleaner = Cleaner.create();
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Object object and store it in resource
Object resource = new Object();
cleaner.register(resource, () -> System.out.println("Cleaned up off-heap resource"));
resource = null;
System.gc();
}
}
Login to try C/C++/Java/PHP code in the editor
StackOverflowError(stack पर बहुत गहरी recursion) कोOutOfMemoryError(heap या metaspace भर गई है) के साथ confuse करना।- किसी static collection में objects जोड़ना और उन्हें कभी न हटाना, जो एक memory leak है क्योंकि वे reachable बने रहते हैं।
- resources release करने के लिए
finalize()पर निर्भर रहना, जबकि इसका समय अप्रत्याशित है; try-with-resources या Cleaner API का उपयोग करें।
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