How to Run Garbage Collector in Java


In this example we will show the method to suggest the JVM to run garbage collection.

Source Code

package com.beginner.examples;


public class RunGCExample {

    public static void main(String[] args){
    	
    	Runtime run = Runtime.getRuntime();
    	//garbage collect
    	run.gc();
		System.out.println("Garbage collect is done!");
    }
    
}

Output:

Garbage collect is done!
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments