How to Collect Garbage in Java


In this example we will show the method to collect garbage in Java.

Source Code

package com.beginner.examples;

public class GC {

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

Output:

OK!
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments