Difference between system.gc() and runtime.gc()

JavaAndroidGarbage Collection

Java Problem Overview


What is the difference between System.gc() and Runtime.gc()?

Java Solutions


Solution 1 - Java

Both are same. System.gc() is effectively equivalent to Runtime.gc(). System.gc()internally calls Runtime.gc().

The only difference is System.gc() is a class method where as Runtime.gc() is an instance method. So, System.gc() is more convenient.

Solution 2 - Java

From looking at the source code: System.gc() is implemented as

Runtime.getRuntime().gc();

So it's just a convenience method.

Solution 3 - Java

See the docs

System.gc() is equivalent to Runtime.getRuntime().gc()

Solution 4 - Java

Runtime.gc() is a native method where as System.gc() is non - native method which in turn calls the Runtime.gc()

Solution 5 - Java

System.gc():

1: It is a class method(static method).

2: Non-Native method.(Code which doesn't directly interacts with Hardware and System Resources).

3: System.gc(), Internally calls Runtime.getRuntime().gc().

Runtime.gc():

1: Instance method.

2: Native method(A programming language which directly interacts with Hardware and System Resources.).

Solution 6 - Java

In the runtime system the gc is instance method but in system method the gc is static .

because of this reason we prefer to use system.gc().

Solution 7 - Java

Both are same System.gc() is effectively equivalent to Runtime.gc()

System.gc() internally calls Runtime.gc().

The only difference is :

System.gc() is a class (static) method where as Runtime.gc() is an instance method. So, System.gc() is more convenient.

System.gc()

public final class System extends Object{

	public static void gc(){
		.
		.
		 Runtime.getRuntime().gc();
	
	}
	.
	.

}

Runtime.gc()

public class Runtime extends Object{

	public void gc(){

	    // ...

	}
	.
	.
	.

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndro SelvaView Question on Stackoverflow
Solution 1 - JavaRamesh PVKView Answer on Stackoverflow
Solution 2 - JavaAndreas DolkView Answer on Stackoverflow
Solution 3 - JavatruthealityView Answer on Stackoverflow
Solution 4 - JavaDebmalya AdhyaView Answer on Stackoverflow
Solution 5 - JavaShivendra PandeyView Answer on Stackoverflow
Solution 6 - JavasravanView Answer on Stackoverflow
Solution 7 - JavaJimmyView Answer on Stackoverflow