File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Know that GC is taking place? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Know that GC is taking place?" Watch "Know that GC is taking place?" New topic

Know that GC is taking place?

Ranch Hand

Joined: May 02, 2001
Posts: 48
Is there anyway to know when garbage collection is taking place?
I have an application that uses lots of objects and bogs down after a certain amount of calculations and I need to know if the time delay is on my end (the JVM) due to waiting for garbage collection or if it's in another part of the application (waiting for database, etc).
I can keep track of how many objects I have and how many are eligible for gc by using the finalize method and a static variable, but other than trying to guess if gc has or is taking place, is there a way to know for sure?
Thanks for any help.
Altaf Ahmad

Joined: Apr 24, 2002
Posts: 13
The finalize() method will be called when garbage collection is taking place. You could put a System.out statement in your finalize() method and see when it gets called.
Ranch Hand

Joined: May 02, 2001
Posts: 48
Actually, the finalize() method is called when the JVM determines that an object is eligible for garbage collection. There is no guarantee that the gc is actually run at that time.
From the API:
After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.

So the object may still be referenced by other objects waiting to finalize. Only after the JVM decides that there aren't any more references will the object be truly eligible for gc. Still doesn't guarantee when that process will run.
However, to answer my own question, I am using Weblogic as my server, so I put in the -verbosegc command in my startup script and so get information that way.
Altaf Ahmad

Joined: Apr 24, 2002
Posts: 13
Yes, you're right. It's run when it's elegible.
My book says that there's no guarantee that it'll be run anyway:
There are no guarantees that the objects no longer in use will be garbage collected and their finalizers executed at all. Garbage collection mihgt not even be run if the program execution does not warrant int. Thus , any memory allocated during program execution might remain allocated after program termination, unless reclaimed by the operating system or by other means.
- A Programmer's Guide to Java Certification.
So, I guess yeah, the finalize() method doesn't really tell you if the garbage collection has taken place.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Invoke java using the -verbose:gc option. This summarizes all GC activity that occurs. You don't get reports on individual objects, but you get the total memory before and after collection, and the time it took to execute each sweep.
As for the finalize() method: it is executed sometime after an object is determined to be eligible for collection, and before it is actually collected. Each of these events may never happen, but can only happen if the preceding events have occurred.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Know that GC is taking place?
It's not a secret anymore!