Harini
Harini

Reputation: 303

What happens when we explicitly call Garbage collector method?

What happens when we explicitly call the Garbage collector method ? I have come across a method gc() which starts the garbage collection, how does this work?

Upvotes: 0

Views: 1677

Answers (2)

solendil
solendil

Reputation: 8468

This method is but a hint to the VM that it could be a suitable time to perform a huge garbage collection. This method could have been useful with the first versions generations of GC when they used to do long pauses, but these times it's quite useless. You're not even guaranteed that there's an actual implementation behind the method :)

Upvotes: 1

yas4891
yas4891

Reputation: 4862

It tells the Garbage collector to do a garbage collection run now.
Note: you can't force the VM into this, but in most circumstances the GC will be run

Here's a link to a blog post on how the Java Garbage Collector works

This question has been asked here

Also have a look at the arguments found here


Update to answer comment: The GC will not collect living objects, even if you're forcing a GC run. It will (mostly) do what it does when run automatically by the JVM

Upvotes: 1

Related Questions