How can I disable Java garbage collector?

后端 未结 8 1150
你的背包
你的背包 2020-12-08 19:16

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

相关标签:
8条回答
  • 2020-12-08 19:35

    Are you sure that it is garbage collection causing the slowdown? Have you run java with -verbose:gc to see what is happening?

    You cannot disable garbage collection on the JVM. You could however look at tuning the garbage collector for better performance.

    0 讨论(0)
  • 2020-12-08 19:37

    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.

    0 讨论(0)
  • 2020-12-08 19:43

    You can use the -Xmx option to set the maximum heap size; using a larger heap should prevent the VM from runnning out of memory and, thereby, requiring garbage collection so soon.

    0 讨论(0)
  • 2020-12-08 19:50

    As everyone as said you can't disable GC in the JVM, which makes sense right, because if you could there'd be memory leaks due to java not having an explicit way for the developer to delete the heap data.

    Do you have access to the source of this java binary? If so it might be worth analysing it and seeing if there's any bottle-necks which could be better written to cut down on GC activity. This could be done with most java profilers, like JProbe for example.

    0 讨论(0)
  • 2020-12-08 19:51

    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.

    0 讨论(0)
  • 2020-12-08 19:52

    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.

    0 讨论(0)
提交回复
热议问题