JVM with no garbage collection

I wanted to find a fast way to keep all objects in memory for a simple initial proof of concept.

The simple way to do this is to run the JVM with a heap that is so large that the GC never needs to run. Set the -Xmx and -Xms options to a large value, and turn on GC logging to confirm that the GC doesn't run for the duration of your test.

This will be quicker and more straightforward than modifying the JVM.


(In hindsight, this may not work. I vaguely recall seeing evidence that implied that the JVM does not always respect the -Xms setting, especially if it was really big. Still, this approach is worth trying before trying some much more difficult approach ... like modifying the JVM.)

Also, this whole thing strikes me as unnecessary (even counter-productive) for what you are actually trying to achieve. The GC won't throw away objects unless they are garbage. And if they are garbage, you won't be able to use them. And the performance of a system with GC disabled / negated is not going to indicative of how a real application will perform.


UPDATE - From Java 11 onwards, you have the much simpler option of using the Epsilon (no-op) garbage collector; see

  • JEP 318: Epsilon: A No-Op Garbage Collector (Experimental)

You add the following options when you launch the JVM:

-XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC

When the heap is filled, no attempt is made to collect garbage. Instead, the Epsilon GC terminates the JVM.


Depending on your needs this could perhaps work:

Using the -Xbootclasspath option you may specify your own implementation of API classes. You could then for instance override the implementation of Object, and add to the constructor, a globalList.add(this) to prevent the objects from being garbage collected. It's a hack for sure, but for simple case-study it's perhaps sufficient.

Another option is to take an open source jvm and comment out the parts that initiate garbage collection. I would guess it is not that complicated.