views:

462

answers:

5

I'd like to register a callback with the JVM so I know when garbage collection is happening. Is there any way to do this?

EDIT: I want to do this so I can log out when garbage collection happens in my application log, so I can see if it correlates to problems I'm seeing. Turning on -Xloggc is helpful, but it is a little tricky to integrate the times from the GC log (which use seconds since app start) into my main application log.

A: 

There's an interesting article on Javalobby discussing one method of doing this.

Kaleb Brasee
Interesting approach, but it introduces some overhead and only notifies you when a specific object is collected. I want a notification when GC happens, at the same frequency that the -Xloggc command logs to a file.
Ted Graham
+2  A: 

It looks like you could use the MemoryPoolMXBean and set the collection usage threshold to 1. This should give you a notification any time the gc runs and there is still at least a byte of memory in use.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/management/MemoryPoolMXBean.html

It looks like this doesn't work with all garbage collectors, though.

Adam Goode
A: 

There is no standard way for your own program to get information from the JVM about garbage collection. Any such API is vendor specific.

Why is the facility you found insufficient?

Thorbjørn Ravn Andersen
The JVM TI is standard for Java 5+ if I recall correctly. Would you like a program to listen to *itself* with that?
Thorbjørn Ravn Andersen
I can't find the ultimate link but the JVM TI seems to be implemented by most JVMs: Sun, IBM (http://publib.boulder.ibm.com/infocenter/javasdk/v6r0/index.jsp?topic=/com.ibm.java.doc.diagnostics.60/diag/tools/jvmti.html), Oracle/BEA, Harmony (and maybe more).
Pascal Thivent
Oh, I see what you meant. But an agent can be your own program :)
Pascal Thivent
Agents do not know of garbage collections. Let me suggest you actually TRY your suggestion of having a program listening to gc's in itself with JVM TI. You might be surprised :)
Thorbjørn Ravn Andersen
A: 

If you're looking at this as a diagnostic tool, I recommend redirecting your application log to StdOut and then redirecting both StdOut and StdErr into a file. This will give you the detail of JVM logging, without forcing you to change your application code.

kdgregory
As I wrote this, I started to wonder if maybe the JVM logging could be redirected into your application logging framework. What are you using for an application logger?
kdgregory
We are using log4j
Ted Graham
+5  A: 

I think that the standard way is to use the JVM Tool Interface (JVM TI) to write an agent with a GC start callback and to log the time from it (see GetTime). Note that a Garbage Collection Start event is sent for full GCs only.

Sample JVM TI agents is available in the demo directory of the JDK 5.0 or the JDK 6 download. The tech article The JVM Tool Interface (JVM TI): How VM Agents Work is another very resource. Also have a look at Creating a Debugging and Profiling Agent with JVMTI.

Pascal Thivent
Good suggestion; I hadn't heard of JVM TI. However, our application almost never does a full gc, so I want to know about the gen 1 collections. What do you think of the MemoryPoolMXBean idea?
Ted Graham