Java Garbage Collection Disabler (Preventer)
TL;DR: Using this code can easily cause the JVM to stop running! Don't use this unless you really know what you are doing!
This class contains methods that are experimental and will often in many cases cause the JVM to freeze and or crash and or cause other unexpected things.
This is Java code that will disable (or at least prevent) the Garbage Collector in Java from running! It does so by locking an object that the GC needs to be able to perform the GC.
This obviously has some bad side effects. But if you know what you are doing, how much memory you have and what your Java program will need in the future, this might be a useful program for you.
Obviously by preventing the GC from running memory will never get released and much [free] memory won't even be able to be used! So make sure you know what you are doing before using this.
This is probably NOT safe for production code, but can be used to play around with and experiment with.
You can for instance use it to prevent the GC from running during critical sections (sections you need to run quickly without any interruptions from the GC) giving you a more exact run time.
You should make sure to enable the GC again before too much happens and the JVM stops running!
This code will probably run on most platforms (Windows, Linux etc.) with OpenJDK and Oracle's JDK versions 8 & 9. However since this program is based on a specific object that the JVM needs to run the GC it might not work on all implementations since they might implement the GC differently or in future version which might change this. This code would probably even run on OpenJDK or Oracle's JDK versions 5-7 if you get rid of the use of Functional Interfaces.
Using this program can easily cause a JVM to crash - or at least stop running!
Note: New objects are allocated to the Eden part of the heap, so even if you set your heap to be 8 gigabytes you can only use the Eden part which is by default considerbly smaller
Examples:
java -Xms8g -Xmx8g example.tmr.utils.UtilsExamples 1
-JVM Status-
Total Memory: 7851 MB
Used Memory: 81 MB
Free Memory: 7769 MB
Max Memory: 7851 MB
1) -----
No GC took 715 milliseconds
No GC took 384 milliseconds
With GC took 364 milliseconds
With GC took 3849 milliseconds
2) -----
No GC took 434 milliseconds
No GC took 362 milliseconds
With GC took 369 milliseconds
With GC took 3751 milliseconds
3) -----
No GC took 477 milliseconds
No GC took 415 milliseconds
With GC took 372 milliseconds
With GC took 3679 milliseconds
4) -----
No GC took 369 milliseconds
No GC took 375 milliseconds
With GC took 381 milliseconds
With GC took 3624 milliseconds
5) -----
No GC took 391 milliseconds
No GC took 363 milliseconds
With GC took 387 milliseconds
With GC took 3593 milliseconds
-JVM Status-
Total Memory: 7851 MB
Used Memory: 1294 MB
Free Memory: 6556 MB
Max Memory: 7851 MB
java -Xms8g -Xmx8g example.tmr.utils.UtilsExamples 2
Before Memory Allocation -JVM Status-
Total Memory: 7851 MB
Used Memory: 81 MB
Free Memory: 7769 MB
Max Memory: 7851 MB
After Memory Allocation -JVM Status-
Total Memory: 7851 MB
Used Memory: 37 MB
Free Memory: 7813 MB
Max Memory: 7851 MB
1) -----
No GC took 554 milliseconds
No GC took 394 milliseconds
With GC took 467 milliseconds
With GC took 3840 milliseconds
2) -----
No GC took 502 milliseconds
No GC took 497 milliseconds
With GC took 374 milliseconds
With GC took 1803 milliseconds
3) -----
No GC took 422 milliseconds
No GC took 489 milliseconds
With GC took 380 milliseconds
With GC took 2402 milliseconds
4) -----
No GC took 410 milliseconds
No GC took 394 milliseconds
With GC took 377 milliseconds
With GC took 2161 milliseconds
5) -----
No GC took 399 milliseconds
No GC took 366 milliseconds
With GC took 374 milliseconds
With GC took 2568 milliseconds
-JVM Status-
Total Memory: 7703 MB
Used Memory: 1305 MB
Free Memory: 6397 MB
Max Memory: 7703 MB
java -Xms8g -Xmx8g example.tmr.utils.UtilsExamples 3
-JVM Status-
Total Memory: 7851 MB
Used Memory: 81 MB
Free Memory: 7769 MB
Max Memory: 7851 MB
-JVM Status-
Total Memory: 7851 MB
Used Memory: 1088 MB
Free Memory: 6762 MB
Max Memory: 7851 MB
Roughly were 985 megabytes are used
-JVM Status-
Total Memory: 7851 MB
Used Memory: 39 MB
Free Memory: 7811 MB
Max Memory: 7851 MB

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
