13

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?

  • It seems to me that this question was already covered: http://stackoverflow.com/questions/2414105/why-is-it-a-bad-practice-to-call-system-gc – default locale Mar 26 '13 at 08:49

6 Answers6

30

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

Nathan White
  • 1,058
  • 6
  • 20
Gokul Nath KP
  • 13,135
  • 24
  • 77
  • 112
  • 9
    +1 I'd also add that call System.gc can actually degrade the performance of the application – MadProgrammer Mar 26 '13 at 08:51
  • 3
    I guess it would be nice to have a list of cases where calling `System.gc()` *is* useful. One case is an attempt to estimate memory usage, during debugging / development (not production). Another case is an unreliable attempt to call finalizers, to analyze memory leaks, again during development. – Thomas Mueller Mar 26 '13 at 09:05
  • 1
    @Thomas, finalizers/class garbage are the prime reasons, finalizers for system resources like DirectBuffers/File descriptors are important ones. Morealso jmap w/ live option does call the garbage collection. There are several -XX options to affect what happens during explicit call (incl. totally nothing). There are some other cases take may have positive effect of having compacted tenured heap (if compaction and tenured are actually an options w/ the chosen collector) – bestsss Mar 27 '13 at 11:42
  • @ThomasMueller There's such a question. Most of the answers are just like "makes no sense", so [I tried to be more constructive](https://stackoverflow.com/a/4785123/581205). – maaartinus Dec 22 '17 at 05:02
  • @maaartinus yes that makes sense. – Thomas Mueller Dec 22 '17 at 14:43
8

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.

Jesper
  • 186,095
  • 42
  • 296
  • 332
  • 4
    *No, it is definitely not good practice.* I'd disagree, if you know why you calling it, it's an ok deal. That would be applicable to some monitoring code, though. I'll give an example calling MappedByteBuffer.map, you need to get it finalized and the only non-hack option does involve the GC. – bestsss Mar 27 '13 at 11:45
  • @bestsss - I interpreted "good practice" as "something that's commonly a good thing to do", and calling the GC manually definitely isn't something that you should normally do - only do it if you really know what you're doing when you can prove to yourself that it is beneficial. – Jesper Mar 27 '13 at 11:53
  • Of course calling for no reason, is a bad idea. btw, a typo in the previous comment: MappedByteBuffer. *unmap* – bestsss Mar 27 '13 at 11:55
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.

Ankur Shanbhag
  • 7,376
  • 2
  • 26
  • 37
5

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.

kaysush
  • 4,559
  • 2
  • 20
  • 43
3

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).

Thilo
  • 241,635
  • 91
  • 474
  • 626
2

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.

prasanth
  • 3,294
  • 4
  • 24
  • 40