import java.awt.*; public class GarbageCollectionExample { public static void main(String[] args) { long startMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); System.out.println("Memory before garbage collection: " + startMemory); System.gc(); long endMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); System.out.println("Memory after garbage collection: " + endMemory); } }
import java.awt.*; public class GarbageCollectionExample { public static void main(String[] args) { Object obj = new Object(); System.out.println("Object before garbage collection: " + obj); System.gc(); System.out.println("Object after garbage collection: " + obj); } }This program creates a new Object and then checks if it gets garbage collected after calling the gc() method. The output will show if garbage collection was successful. Package Library: java.awt