Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When garbage collector is invoked

 
Arka Sharma
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to know how garbage collector is invoked ?Is it invoked periodically or some memory threshold is there in generations after which respective gc for that generation will be invoked ?Can we schedule garbage collection by setting VM flags apart from using System.gc() or Runtime.gc() ?

Thanks,
Arka
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can never say when Garbage collection really happens. You can just instruct that you desire to run garbage collection using the gc() method.

If you do a search for garbage collection in this forum I hope you will get good helpful answers.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15276
38
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally you don't need to know, you just let the garbage collector do its work, and you can't really predict when exactly it happens. When the garbage collector runs exactly depends on the implementation details of the JVM. In the course of time there have been different garbage collector algorithms implemented in different versions of Sun's and Oracle's JVM, so the answer to your question is most likely different for different Java versions.

You cannot schedule garbage collection and you normally should not be concerned with that. Calling System.gc() suggests to the JVM that it might be a good time to do garbage collection, but you should be very careful with using that method. Only use it if you have some kind of proof that it will actually help. If you call it at the wrong time or too often, it might even hurt performance of your application.

Tuning garbage collection is an advanced subject. You can find some info on it here (for Oracle's HotSpot JVM version 6): Java SE 6 HotSpot[tm] Virtual Machine Garbage Collection Tuning
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is making object references null any better than calling System.gc()?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both are different. Making references to null make the particular non-referenced object eligible for garbage collection. Where as gc() method indicates that Garbage collector shall be run.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:Where as gc() method indicates that Garbage collector shall be run.

Not exactly. It's a request to run the garbage collector, but the JVM may ignore that request.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Rob. Thanks. I understand that..

Might be better if the docs said the request could be ignored.

Source - http://download.oracle.com/javase/6/docs/api/java/lang/System.html#gc()
When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15276
38
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides the part that you quoted, the API docs for System.gc() also say:
API docs wrote:Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects...

The word "suggests" means that you're only providing a hint to the JVM. The JVM may or may not take the hint and immediately do garbage collection.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay Jesper
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've use this once in a production project. The case then was that whenever we started the big batch job, we needed to free as much memory as possible. So we had a listener interface with a callback that made the different parts of the server go into hibernation, free as much resources as they possibly could and then report back to the BFBJ (Big Fat Batch Job). When everyone had gone to sleep, we run the GC in a loop until we got enough space to play with. Then we started the BFBJ and kept an eye on the memory. Whenever we got to close to the upper limit, we tried to free more memory by suggesting that the GC would run.

In this case, it was most important that the BFBJ was run without crashing the server than doing it quickly. So basically what we did was to try to get as much garbage out on the street and then calling for a garbage truck over and over again.

But in this case, we traded performance for stability. Today, there are much more sophisticated ways of doing it.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ove Lindström wrote:we run the GC in a loop until we got enough space to play with.

Each time the GC ran (how did you noticed it did run?) were there free spaces generated... I mean why it was not able to free all garbage collectible memory in one run... Just curious if you can give little more useful information.
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:
Ove Lindström wrote:we run the GC in a loop until we got enough space to play with.

Each time the GC ran (how did you noticed it did run?) were there free spaces generated... I mean why it was not able to free all garbage collectible memory in one run... Just curious if you can give little more useful information.


We had no direct way of knowing that the GC had done a sweep other than looking at the free memory. Today, you could use the MX-beans in the java.lang.management package. The MemoryManagerMXBean has a counter that can be used to detect if GC has done a sweep. This does not mean that any objects actually has been removed. Depending on the algorithm, most GC needs at least two runs before they erase an object. I think that if I was to do the same "stunt" today, I would opt for buying/creating a custom GC that don't stop until it is done.

Addition: I didn't say that it was a good solution... just that we did it. I wonder when this kind of "crime" is deleted from the files... ;)
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Thank you for sharing that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic