What is the 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.
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.
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.
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.
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.
From looking at the source code: System.gc()
is implemented as
Runtime.getRuntime().gc();
So it's just a convenience method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With