How can I disable Java garbage collector?

48,372

Solution 1

There is no way to disable garbage collection entirely. Garbage collection is only run when the JVM runs out of space, so you could give the program more memory. Add these command line options to the Java command

-Xmx256M -Xms256M

This gives the program 256Mb of ram (the default is 64Mb). Garbage collection will not take 3 seconds for a default size JVM though, so you might want to investigate more closely what the program is doing. Yourkit profiler is very useful for figuring out what is taking a long time.

Solution 2

It sounds like you are trying to save time, but going about it the wrong way. The time saved in disabling garbage collection would be trivial (for a single task) compared to the time taken to launch and shutdown the java process. You might want to consider having a java process launch that you can ask multiple times to do the work you require if run-time performance is your goal.

Solution 3

GC only kicks in when JVM is short on memory, so you either GC or die. Try turning on verbose GC and see if it actually takes significant amount of time.

java -verbose:gc

Solution 4

Java 11 comes with an no-op garbage collector.

It can be enabled by the -XX:+UseEpsilonGC option at JVM start.

According to the JEP decription one of its goals is to make certain short-lived jobs more efficient, what might be you use case:

Extremely short lived jobs. A short-lived job might rely on exiting quickly to free the resources (e.g. heap memory). In this case, accepting the GC cycle to futilely clean up the heap is a waste of time, because the heap would be freed on exit anyway. Note that the GC cycle might take a while, because it would depend on the amount of live data in the heap, which can be a lot.

Solution 5

Java 11 gives you the binary option to either have Java GC on, or have Java GC turned off. To turn off Java GC you use the Epsilon Garbage Collector which must be turned off on the command line. On Java 11, use the following two JVM arguments:

-XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC

Without the UnlockExperimentalVMOptions argument, the JVM will fail to start, so make sure it's included.

Can't Stop Java GC

Unfortunately, if you're not using Epsilon GC, there is no way to disable, stop or prevent garbage collection from happening. Just like not being able to trigger GC, you can't stop Java GC either. The algorithms are non-deterministic. Only the JVM can control when they occur.

Share:
48,372
Nelson
Author by

Nelson

Updated on January 15, 2021

Comments

  • Nelson
    Nelson over 3 years

    We have a PHP webapp that calls a Java binary to produce a PDF report (with JasperReports). The Java binary outputs the PDF to standard output and exits; the PHP then sends the PDF to browser. This Java command lasts about 3 to 6 seconds, and I think when it lasts 6 second it's because the GC kicks in. I would like to disable the GC because anyway when the command exits all memory is returned.

    I would like to know how to disable it for Java 1.4.2 and for Java 1.6.0 because we are currently testing both JVM to see which performs faster..

  • Nelson
    Nelson almost 14 years
    Umm, then maybe I could use a commandline switch to give it more memory and so increase the chance the GC does not kicks in..
  • unbeli
    unbeli almost 14 years
    Yes, you will know if you need to do that after analyzing the output of verbose:gc. That's the first step.
  • gustafc
    gustafc almost 14 years
    +1 for "test, don't guess". The time diff could be something completely unrelated, like I/O contention or some CPU heavy process kicking in.
  • Stephen C
    Stephen C almost 14 years
    Just setting -Xmx won't make any difference. You actually need to set -Xms as well since you want to control the initial heap size.
  • Nelson
    Nelson almost 14 years
    Yes, this is good approach although more work to do, I would have to code a kind of daemon that would listen on sockets and so on.. besides this "daemon" would have to be restarted once in X days.. cause java leaks memory in the long run (from my experience)..
  • Trevor Tippins
    Trevor Tippins almost 14 years
    @Nelson Consider seeing if you can use something like Mule to host your Java processes and let that handle all of the communications for you. The idea being that you just need to embed your JasperReports as a service that the ESB can host and route requests to as necessary. Mule: mulesoft.com/mule-esb-open-source-esb
  • Nelson
    Nelson almost 14 years
    the java binary is very simple, it just retrieve commandline paraemeters and make calls into jasperreport functions to create the report, which is passed to the System.out outputstream where the PHP read it through 'passthrough' function. So the the time is spent on Jasperreport proccessing the report, thats how I wanted a java general speedup.
  • display101
    display101 almost 14 years
    It looks like controlling the JVM GC settings is your only avenue here then.
  • Hardcoded
    Hardcoded almost 14 years
    @Nelson Java doesn't leak memory per se. We have java deamons running for months, only restarted for application updates or system downtimes. If your Java programs are leaking, try to use a profiler. There are other options than open the socket yourself. There is ActiveMQ/JMS, JMX, or you could create a webservice for it, maybe jetty would be interesting.
  • earizon
    earizon over 4 years
    I can think of other important use cases: youtube.com/watch?v=zQVytExlnEk