I want to know if there is a way to stop and start the JVM performing Garbage Collection during runtime.
If there is not, why not? Surely this feature would make Java more suitable for safety critical applications?
I want to know if there is a way to stop and start the JVM performing Garbage Collection during runtime.
If there is not, why not? Surely this feature would make Java more suitable for safety critical applications?
Actually, there is a way to stop Java GC. Just use the Epsilon GC algorithm that was introduced as an experimental feature in Java 11. Just add the following two arguments to your JVM's startup script:
-XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC
Now just keep in mind that this Java GC algorithm does no GC at all. So if you do any object allocation in your code, eventually you'll hit an OutOfMemoryError
and your app will crash. But if your JVM is short lived, and you don't think that's an issue, give Epsilon GC a try.
Just remember it's all or nothing. You can't force Java GC and you can't stop Java GC from happening if you use any of the other garbage collectors. The collector is non-deterministic, so control by programmers or admins just isn't possible out of the box.
By default the JVM runs the JVM only needed. This means you can't turn off the GC or your program will fail.
The simplest way to avoid stopping the JVM is;