Introduction
Garbage collection helps automatically clean up memory that your program no longer uses, so your computer doesn't run out of space.
No direct syntax to call garbage collection; it runs automatically.
You can suggest it by calling:
System.gc();System.gc();// Create objects String text = new String("Hello"); // After text is no longer used, garbage collector can free its memory
public class GarbageCollectionDemo { public static void main(String[] args) { // Create an object String message = new String("Hello, world!"); // Remove reference message = null; // Suggest garbage collection System.gc(); System.out.println("Garbage collection suggested."); } }
public class TestGC {
public static void main(String[] args) {
String s = new String("hello");
s = null;
System.gc();
System.out.println("Done");
}
}public class GCExample {
public static void main(String[] args) {
Object obj = new Object();
obj = null;
System.gc;
}
}Pass the test and this pattern moves unknown