Java Force Garbage Collection – Code Example

Garbage collection in java can not be enforced. But still sometimes, we call the System.gc( ) method explicitly. System.gc() method provides just a "hint" to the JVM that garbage collection should run. It is not guaranteed!

The API documentation for System.gc() states that "When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects." The responsibility for a programmer is to make sure no references remain for objects.

The program below shows that the garbage collection may run after System.gc() is called. Again, it is NOT guaranteed.

public class GCTest {
	public static void main(String[] args) throws InterruptedException {
		A a = new A("white");
		a = null;
 
		Runtime.getRuntime().gc();
	}
}
 
class A {
	private String color;
 
	public A(String color) {
		this.color = color;
	}
 
	@Override
	public void finalize() {
		System.out.println(this.color + " cleaned");
	}
}

So the program may or may not output the following:

white cleaned

The program below shows that the garbage collection automatically works behind without System.gc() called.

public class GCTest {
	public static void main(String[] args) throws InterruptedException {
		A a = new A("white");
 
		for (int i = 0; i < 10000000; i++) {
			if (i % 2 == 1) {
				a = new A("red");
			} else {
				a = new A("green");
			}
			a = null;
		}
	}
}
 
class A {
	private String color;
 
	public A(String color) {
		this.color = color;
	}
 
	@Override
	public void finalize() {
		System.out.println(this.color + " cleaned");
	}
}

It will output something like the following:

...
green cleaned
green cleaned
red cleaned
red cleaned
green cleaned
green cleaned
red cleaned
red cleaned
green cleaned
green cleaned
red cleaned
red cleaned
...
Category >> Memory  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>

  1. JP @ java interview question on 2011-7-21

    As per my understanding you can not force garbage collection java you can only make request.

  2. tippu on 2012-4-13

    Can we override System.gc() method

  3. Admin on 2012-4-20

    System class is declared as a final class, (public final class System), so it can not be overridden.

  4. Luis Alberto Romero Calderon on 2013-10-28

    I recommend you to use a highlight syntax it’s much better read the code.

  5. ryanlr on 2013-10-28

    Thanks a lot for pointing this out.

  6. Luis Alberto Romero Calderon on 2013-10-29

    much better. 🙂

  7. Evan Bechtol on 2015-5-18

    This is honestly not good practice; calling System.gc()

    It’s a sign that your code is fundamentally flawed somewhere and needs serious revising.
    It also implies that the developer expects something to be cleaned, but as you state it may not necessarily be cleaned. This can result is some serious repercussions later in your program if you write code that is dependent on that object being picked up by gc.

    Also, the title is a bit misleading, you can NEVER explicitly force the garbage collection to run.

    Overall, calling garbage collection manually is HIGHLY inadvisable.

Leave a comment

*