Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JVM with no garbage collection

Tags:

I've read in many threads that it is impossible to turn off garbage collection on Sun's JVM. However, for the purpose of our research project we need this feature. Can anybody recommend a JVM implementation which does not have garbage collection or which allows turning it off? Thank you.

like image 565
H-H Avatar asked Jun 05 '10 10:06

H-H


People also ask

What will happen if there is no garbage collection in Java?

If you don't have enough memory to run your application, you will experience slowdowns, long garbage collection times, "stop the world" events, and eventually out of memory errors. This can indicate that your heap is too small, but can also mean that you have a memory leak in your application.

Does the JVM do garbage collection?

Java garbage collection is an automatic process. The programmer does not need to explicitly mark objects to be deleted. The garbage collection implementation lives in the JVM. Each JVM can implement garbage collection however it pleases; the only requirement is that it meets the JVM specification.

What is the alternative to garbage collection in Java?

Another very fast approach is to have dedicated object pools for different classes of object. Released objects can just be recycled in the pool, using something like a linked list of free object slots. Operating systems often used this kind of approach for common data structures.


2 Answers

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.

like image 79
Stephen C Avatar answered Sep 19 '22 20:09

Stephen C


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.

like image 32
aioobe Avatar answered Sep 23 '22 20:09

aioobe