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
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 good resource. Also have a look at Creating a Debugging and Profiling Agent with JVMTI.