There is gc()
method both in System & Runtime classes, and System class's gc()
method calls that of Runtime.
public static void gc() {
Runtime.getRuntime().gc();
}
As per Java code documentation for gc() in Runtime class
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. When control returns from the method call, the virtual machine has made its best effort to recycle all discarded objects.
Is there anything special that happens when Runtime.getRuntime().gc()
is called?
What will happen if I call it and do not call it?
What I already know?
Calling gc()
do not invoke Garbage Collector instantly and its upto jvm when Garbage Collection should happen.
EDIT
I want to understand why gc()
is still there in Java8. Discussions on other thread do not have accepted answer and are very old.