Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 323
Andro Selva Avatar asked Jun 01 '11 06:06

Andro Selva


People also ask

What is difference between system gc () and runtime gc ()?

The only difference is : System. gc() is a class (static) method where as Runtime. gc() is an instance method.

What does System gc and runtime gc method do?

System gc and Runtime gc are two methods to request JVM to run Garbage collector. The basic difference between System gc and Runtime gc in Java is that System gc is a class method while Runtime gc is an instance method. Usually, System gc is more convenient than Runtime gc.

What is runtime gc?

gc() method runs the garbage collector. Calling this method suggests that the Java virtual machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse.

What is runtime and garbage collector?

In the common language runtime (CLR), the garbage collector (GC) serves as an automatic memory manager. The garbage collector manages the allocation and release of memory for an application. For developers working with managed code, this means that you don't have to write code to perform memory management tasks.


2 Answers

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.

like image 164
Ramesh PVK Avatar answered Sep 28 '22 03:09

Ramesh PVK


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

Runtime.getRuntime().gc(); 

So it's just a convenience method.

like image 26
Andreas Dolk Avatar answered Sep 28 '22 03:09

Andreas Dolk