This question already has an answer here:
-
Why is it bad practice to call System.gc()?
13 answers
My application has lot of iterations. Till now I haven't faced any memory issues. But from code level I can suspect there are few places, which causes memory leaks and out of memory problem. I am thinking of calling garbage collector manually. Is it good practice to call Garbage Collector manually?
You can call Garbage collector using:
System.gc();
But this does not mean that it'll be executed immediately. The JVM decides when to execute it. In general if the JVM is about to throw an OutOfMemoryError
, calling System.gc()
won't prevent it. Better investigate why you're leaking so much memory and clean it up along the way.
JavaDoc:
Calling the gc 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 Java Virtual Machine has made a best effort
to reclaim space from all discarded objects
Is it good practice to call Garbage Collector manually?
No, it is definitely not good practice.
You can use System.gc()
. Note that this is not guaranteed to call the garbage collector - it only gives a hint to the system that it might be a good idea to do garbage collection.
The garbage collector in Oracle's JVM contains a lot of sophisticated logic to determine when and what to cleanup. Tuning it requires knowledge about details on how it works. Just putting a System.gc()
somewhere in your program is not likely to help a lot, and in fact, it can even make it worse.
See Java SE 6 HotSpot Virtual Machine Garbage Collection Tuning for details on how to tune garbage collection with Java SE 6.
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. When you make call using System.gc(), it is just a request to JVM and JVM can anytime decide to ignore it.
Yes you can explicitly call garbage collector using
System.gc();
But what happens is that you can't order JVM to do garbage collection immediately. JVM decides on its own when to garbage collect.Hence its not a good idea of calling it manually.
Moreover regarding OutOfMemoryException
doing manual garbage collection won't help you prevent the exception, since JVM throws this exception after reclaiming all the memory it can. It has some very sophisticated algorithms to determine when and how to do perform the garbage collection.
So what i suggest is that if you are getting OutOfMemoryException
then recheck your program make it more efficient or increase heap space.
Regardless of whether you can or cannot manually trigger the garbage collector (and the different levels of collection), and what impact this has on performance (which indeed is a topic worth discussion), it will not prevent OutOfMemoryErrors, because when the JVM is about to run out of memory, it does the most thorough collection it can anyway. Only if after that collection not enough memory is available, will it error out. Even if you trigger the collection earlier yourself, the result (the amount of memory reclaimed) is the same.
Memory leaks cannot be fixed by running garbage collection more often.
They have to be fixed in your program (stop referencing things you don't need anymore earlier), or (worst case, if it is a "real" leak) in the JVM or runtime library itself (but genuine memory management bugs should not exist anymore after so many years of service).
Calling System.gc() does not guarantee any GC. Garbage is collected if there is a real need for memory.
You can have a look at different garbage collectors here.
http://javarevisited.blogspot.in/2011/04/garbage-collection-in-java.html
You can include any of these GCs in your command line params as per your needs.