title: Java: How do you really force a GC using JVMTI's ForceGargabeCollection?
body: <p>I'm not looking for the usual "you can only hint the GC in Java using <em>System.gc()</em>" answers, this is not at all what this question is about.</p>
  
  <p>My questions is not subjective and is based on a reality: GC <strong>can</strong> be forced in Java for a fact.  A lot of programs that we use daily do it: IntelliJ IDEA, NetBeans, VisualVM.</p>
  
  <p>They all can <strong>force</strong> GC to happen.</p>
  
  <p>How is it done?  </p>
  
  <p>I take it they're all using JVMTI and more specifically the <em>ForceGarbageCollection</em> (notice the "Force") but how can I try it for myself?</p>
  
  <p><a href="http://java.sun.com/javase/6/docs/platform/jvmti/jvmti.html#ForceGarbageCollection" rel="noreferrer">http://java.sun.com/javase/6/docs/platform/jvmti/jvmti.html#ForceGarbageCollection</a></p>
  
  <p>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.</p>
  
  <p>The question is really "how do you force a GC using JVMTI's ForceGarbageCollection"?</p>
  
  <p>Does the JVM needs to be launched with any special parameters?</p>
  
  <p>Is any JNI required?  If so, what code exactly?</p>
  
  <p>Does it only work on Sun VMs?</p>
  
  <p>Any complete and compilable example would be most welcome.</p>