Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling System.gc( ) explicitly?

It is said that we cannot force the garbage collection process in java.
It's after all, a daemon thread.

But still sometimes, why we call the System.gc( ); function explicitly ?
Is it worth calling it ? Any Pro's and Con's ?
If not useful in many situations, why this method is not deprecated from Java ?

PS : Explanation with an example will be useful

like image 300
Saurabh Gokhale Avatar asked Jan 24 '11 17:01

Saurabh Gokhale


People also ask

Can we call GC explicitly?

You can call Garbage Collector explicitly, but JVM decides whether to process the call or not. Ideally, you should never write code dependent on call to garbage collector. JVM internally uses some algorithm to decide when to make this call.

What happens when we call System GC ()?

gc() method runs the garbage collector. Calling this 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.

How can we call the garbage collector explicitly with an example?

Java gc() method is used to call garbage collector explicitly. However gc() method does not guarantee that JVM will perform the garbage collection. It only request the JVM for garbage collection. This method is present in System and Runtime class.

When should you call System GC?

There are very few reasons when an explicit call to the System. gc() method might be useful. One possible reason is cleaning memory after server startup — we're starting a server or application which does a lot of preparation. After that, there are a lot of objects to be finalized.


2 Answers

The best way, in my opinion, to think of the System.gc() method is as a "hint" to the VM that garbage collection should run. That said, much like a large percentage of "optimizations" people think they are performing, it's usually best to just let the system take care of things on its own. Systems are evolving etc, etc, etc. There are still some instances where the developer may actually know better and the use case for it is probably very similar to why some code is still written in assembly (most of the time, the compiler is better, but in a few instances -- or with a few developers -- humans can actually write more efficient code).

One example I've seen used in the past to justify its existence is in the event that a large number of objects were allocated and you as the developer know the instant they are no longer going to be used. In that case, you may have more information about the memory utilization than the GC does (or at least, before it realizes it) and, since the amount of memory reclaimed will be significant, it makes sense to suggest that it runs.

like image 75
Chris Thompson Avatar answered Oct 09 '22 05:10

Chris Thompson


You answered half of your question: is it worth it? No, because you can't force it.

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.

This is almost EULA-English for "you can try but we both know the outcome". :)

Furthermore, application servers can (and often will) disable it using the -XX:-DisableExplicitGC command line option.

So what's the point then? Well, there could be one: some applications like to display the amount of free memory available on the heap, and before you refresh your display, it could be moderately useful to call System.gc(); before it.

like image 44
biziclop Avatar answered Oct 09 '22 03:10

biziclop