This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi, 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. April
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. Altaf
Joined: May 02, 2001
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.
Joined: Apr 24, 2002
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.
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.