Upper GC in Java
Upper GC in Java
- by utk
Java handles memory management so you don't have to explicitly free() or delete()
objects (unlike C/C++).
🔹 Key Concepts
1. Heap Memory
2. Reachability
java
CopyEdit
String s = new String("Hello");
s = null; // Now "Hello" is eligible for GC
Use only in specific test or tuning cases. The JVM decides the best time to collect.
🔹 Example of GC in Action
java
CopyEdit
public class GCDemo {
public static void main(String[] args) {
GCDemo obj = new GCDemo();
obj = null; // Now eligible for GC
// Requesting GC
System.gc();
}
@Override
protected void finalize() throws Throwable {
System.out.println("Garbage Collected!");
}
}
🔹 Best Practices