Java: How do you really force a GC using JVMTI's ForceGargabeCollection?

Posted by WizardOfOdds on Stack Overflow See other posts from Stack Overflow or by WizardOfOdds
Published on 2010-02-01T16:34:01Z Indexed on 2010/03/30 17:43 UTC
Read the original article Hit count: 440

Filed under:
|
|

I'm not looking for the usual "you can only hint the GC in Java using System.gc()" answers, this is not at all what this question is about.

My questions is not subjective and is based on a reality: GC can be forced in Java for a fact. A lot of programs that we use daily do it: IntelliJ IDEA, NetBeans, VisualVM.

They all can force GC to happen.

How is it done?

I take it they're all using JVMTI and more specifically the ForceGarbabeCollection (notice the "Force") but how can I try it for myself?

http://java.sun.com/javase/6/docs/platform/jvmti/jvmti.html#ForceGarbageCollection

Also note that this question is not about "why" I'd want to do this: the "why" may be "curiosity" or "we're writing a program similar to VisualVM", etc.

The question is really "how do you force a GC using JVMTI's ForceGarbageCollection"?

Does the JVM needs to be launched with any special parameters?

Is any JNI required? If so, what code exactly?

Does it only work on Sun VMs?

Any complete and compilable example would be most welcome.

© Stack Overflow or respective owner

Related posts about java

Related posts about garbage-collection