Can we call the Garbage Collector more than once in a single Java program?
-
2Sure. The JVM itself does it all the time. – Federico klez Culloca Sep 30 '20 at 07:25
-
1You can. But you shouldn't. What makes you think you need to? – Fildor Sep 30 '20 at 07:25
-
1Why don't you try it and see? – Scary Wombat Sep 30 '20 at 07:25
-
Why can't we do it more than once? Like is there any reason? No, I was just curious – Aditya Ramachandran Sep 30 '20 at 07:26
-
1@AdityaRamachandran Did you read the comments? You **can**. But you **should not**. – Fildor Sep 30 '20 at 07:27
-
Yeah read it... – Aditya Ramachandran Sep 30 '20 at 07:28
-
4@AdityaRamachandran please follow this discussion, this may help you. https://stackoverflow.com/questions/2414105/why-is-it-bad-practice-to-call-system-gc – Simmant Sep 30 '20 at 07:29
-
1_"Yeah read it"_ - then why do you ask _"Why can't we do it more than once?"_ ? :D It's generally not a good idea to do that. There are very rare cases where it is beneficial. If you do, you should _really_ know what and why you are doing it and prove that it actually _is_ beneficial. Otherwise it will (probably) just additionally reduce performance in your app. – Fildor Sep 30 '20 at 07:32
-
Sorry for asking the question again though! My first time on StackOverflow – Aditya Ramachandran Sep 30 '20 at 07:36
-
@Holger.., I did apologise right?? it happens sometimes and yeah I agree that I shouldn't have asked it but I also apologised for asking the question. Well, and I didn't ignore the answers so yeah, It makes no sense of you to come here after all this had ended just to give me a piece of advice but yeah thank you I guess... – Aditya Ramachandran Oct 02 '20 at 09:11
-
Okay, then I apologise again! Sorry – Aditya Ramachandran Oct 02 '20 at 09:17
1 Answers
If by "call the Garbage Collector" you mean "invoke System.gc()
-- you can do it as often as you like. It may have some effect, or it may have none. Depending on the circumstances, it may make your application perform better, or it may make things worse.
Modern Java implementations are designed to run with completely transparent GC. My experience is that it rarely helps to poke the GC into action although, nearly always, calling gc()
does actually elicit some sort of response from the JVM -- it's not usually ignored completely.
I've seen applications that use repeated calls to gc()
to overcome bugs in the application (usually related to resource management); or even, very occasionally, bugs in the JVM.
On balance, though, I expect that if you find your application needs to poke the GC often, something is broken and, these days, it's most likely the application.

- 4,092
- 1
- 11
- 15
-
imho: an excellent answer, simple and to the point. `System::gc` is used also for proving some things in tests or sandboxes, for example, too. Like a case when a `WeakReference` is cleared, etc. There are also flags that control what `System::gc` is going to do : `DisableExplicitGC` and `ExplicitGCInvokesConcurrent` – Eugene Sep 30 '20 at 14:44
-
1It doesn’t need to be a “modern Java implementation”. Calling `gc()` manually never was required. – Holger Oct 01 '20 at 08:20
-
@Holger -- in principle, yes. However I can remember having to help out the GC back in the early days of Java. – Kevin Boone Oct 01 '20 at 10:31
-
1When you want to make the stats look good without actually needing the memory. Actual allocations always triggered a gc when no memory was available. That worked in JDK 1.1 for sure. I didn’t test 1.0 enough to say that for sure, though… Well, the only exception is when having lots of objects with a `finalize()` method, though. As garbage collection doesn’t imply finalization, so running gc in advance could make a difference then. – Holger Oct 01 '20 at 10:34