File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes When garbage collector is invoked Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "When garbage collector is invoked" Watch "When garbage collector is invoked" New topic
Author

When garbage collector is invoked

Arka Sharma
Ranch Hand

Joined: Jun 15, 2011
Posts: 103
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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Aug 16, 2005
Posts: 14150
    
  18

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

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
is making object references null any better than calling System.gc()?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Aug 16, 2005
Posts: 14150
    
  18

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
Bartender

Joined: May 31, 2011
Posts: 1776
Okay Jesper
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Mar 10, 2008
Posts: 326

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
Bartender

Joined: May 31, 2011
Posts: 1776
Ok. Thank you for sharing that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When garbage collector is invoked