Sobes.tech
Junior — Middle

Is it possible to programmatically initiate garbage collection in JVM?

sobes.tech AI

Answer from AI

In JVM, you can programmatically initiate garbage collection by calling the System.gc() method. This call merely recommends the JVM to run the garbage collector, but does not guarantee immediate execution. The JVM independently decides when exactly to perform garbage collection based on current load and memory state.

Example:

public class GCDemo {
    public static void main(String[] args) {
        System.gc(); // Request to run garbage collector
    }
}