There's a GC start/end event in JVM TI: http://docs.oracle.com/javase/7/docs/platform/jvmti/jvmti.html#GarbageCollectionStart It's been around for quite a while. Can be used in JDK 6. Can't execute Java code in the callback. The VM is in its stop-the-world phase when the event is sent.
From the document:
This event is sent while the VM is still stopped, thus the event handler must not use JNI functions and must not use JVM TI functions except those which specifically allow such use (see the raw monitor, memory management, and environment local storage functions).
The places that issue this event:
>Internal search for "GarbageCollectionStart" in "*.c *.cxx *.h *.cpp *.hpp *.cc *.ad"
openjdk7b147/hotspot/src/share/vm/prims/jvmtiExport.cpp:1945: jvmtiEventGarbageCollectionStart callback = env->callbacks()->GarbageCollectionStart;
>
>Internal search for "garbage_collection_start" in "*.c *.cxx *.h *.cpp *.hpp *.cc *.ad"
openjdk7b147/hotspot/src/share/vm/prims/jvmtiEventController.cpp:75:static const jlong GARBAGE_COLLECTION_START_BIT = (((jlong)1) << (JVMTI_EVENT_GARBAGE_COLLECTION_START - TOTAL_MIN_EVENT_TYPE_VAL));
openjdk7b147/hotspot/src/share/vm/prims/jvmtiEventController.cpp:606: JvmtiExport::set_should_post_garbage_collection_start((any_env_thread_enabled & GARBAGE_COLLECTION_START_BIT) != 0);
openjdk7b147/hotspot/src/share/vm/prims/jvmtiExport.cpp:854:bool JvmtiExport::_should_post_garbage_collection_start = false;
openjdk7b147/hotspot/src/share/vm/prims/jvmtiExport.cpp:1932:void JvmtiExport::post_garbage_collection_start() {
openjdk7b147/hotspot/src/share/vm/prims/jvmtiExport.cpp:1934: EVT_TRIG_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_START,
openjdk7b147/hotspot/src/share/vm/prims/jvmtiExport.cpp:1939: if (env->is_enabled(JVMTI_EVENT_GARBAGE_COLLECTION_START)) {
openjdk7b147/hotspot/src/share/vm/prims/jvmtiExport.cpp:1940: EVT_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_START,
openjdk7b147/hotspot/src/share/vm/prims/jvmtiExport.cpp:2433: if (JvmtiExport::should_post_garbage_collection_start()) {
openjdk7b147/hotspot/src/share/vm/prims/jvmtiExport.cpp:2434: JvmtiExport::post_garbage_collection_start();
openjdk7b147/hotspot/src/share/vm/prims/jvmtiExport.hpp:106: JVMTI_SUPPORT_FLAG(should_post_garbage_collection_start)
openjdk7b147/hotspot/src/share/vm/prims/jvmtiExport.hpp:309: static void post_garbage_collection_start() KERNEL_RETURN;
>
>Internal search for "JvmtiGCMarker" in "*.c *.cxx *.h *.cpp *.hpp *.cc *.ad"
openjdk7b147/hotspot/src/share/vm/gc_implementation/shared/vmGCOperations.hpp:216: JvmtiGCMarker _jgcm;
openjdk7b147/hotspot/src/share/vm/prims/jvmtiExport.cpp:2427:JvmtiGCMarker::JvmtiGCMarker() {
openjdk7b147/hotspot/src/share/vm/prims/jvmtiExport.cpp:2443:JvmtiGCMarker::~JvmtiGCMarker() {
openjdk7b147/hotspot/src/share/vm/prims/jvmtiExport.hpp:474:class JvmtiGCMarker : public StackObj {
openjdk7b147/hotspot/src/share/vm/prims/jvmtiExport.hpp:476: JvmtiGCMarker() KERNEL_RETURN;
openjdk7b147/hotspot/src/share/vm/prims/jvmtiExport.hpp:477: ~JvmtiGCMarker() KERNEL_RETURN;
>
>Internal search for "SvcGCMarker" in "*.c *.cxx *.h *.cpp *.hpp *.cc *.ad"
openjdk7b147/hotspot/src/share/vm/gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.cpp:6320: SvcGCMarker sgcm(SvcGCMarker::OTHER);
openjdk7b147/hotspot/src/share/vm/gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.cpp:6328: SvcGCMarker sgcm(SvcGCMarker::OTHER);
openjdk7b147/hotspot/src/share/vm/gc_implementation/g1/concurrentMark.cpp:1196: SvcGCMarker sgcm(SvcGCMarker::OTHER);
openjdk7b147/hotspot/src/share/vm/gc_implementation/g1/g1CollectedHeap.cpp:1170: SvcGCMarker sgcm(SvcGCMarker::FULL);
openjdk7b147/hotspot/src/share/vm/gc_implementation/g1/g1CollectedHeap.cpp:3197: SvcGCMarker sgcm(SvcGCMarker::MINOR);
openjdk7b147/hotspot/src/share/vm/gc_implementation/parallelScavenge/vmPSOperations.cpp:45: SvcGCMarker sgcm(SvcGCMarker::MINOR);
openjdk7b147/hotspot/src/share/vm/gc_implementation/parallelScavenge/vmPSOperations.cpp:67: SvcGCMarker sgcm(SvcGCMarker::FULL);
openjdk7b147/hotspot/src/share/vm/gc_implementation/parallelScavenge/vmPSOperations.cpp:88: SvcGCMarker sgcm(SvcGCMarker::FULL);
openjdk7b147/hotspot/src/share/vm/gc_implementation/shared/vmGCOperations.cpp:163: SvcGCMarker sgcm(SvcGCMarker::MINOR);
openjdk7b147/hotspot/src/share/vm/gc_implementation/shared/vmGCOperations.cpp:176: SvcGCMarker sgcm(SvcGCMarker::FULL);
openjdk7b147/hotspot/src/share/vm/gc_implementation/shared/vmGCOperations.cpp:184: SvcGCMarker sgcm(SvcGCMarker::FULL);
openjdk7b147/hotspot/src/share/vm/gc_implementation/shared/vmGCOperations.hpp:214:class SvcGCMarker : public StackObj {
openjdk7b147/hotspot/src/share/vm/gc_implementation/shared/vmGCOperations.hpp:220: SvcGCMarker(reason_type reason ) {
openjdk7b147/hotspot/src/share/vm/gc_implementation/shared/vmGCOperations.hpp:224: ~SvcGCMarker() {