如何禁用 Java 垃圾收集器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2927396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I disable Java garbage collector?
提问by Nelson
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.
我们有一个 PHP 网络应用程序,它调用 Java 二进制文件来生成 PDF 报告(使用 JasperReports)。Java 二进制文件将 PDF 输出到标准输出并退出;PHP 然后将 PDF 发送到浏览器。这个 Java 命令持续大约 3 到 6 秒,我认为它持续 6 秒是因为 GC 启动。我想禁用 GC,因为无论如何当命令退出时,所有内存都会返回。
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..
我想知道如何为 Java 1.4.2 和 Java 1.6.0 禁用它,因为我们目前正在测试两个 JVM 以查看哪个执行速度更快。
采纳答案by Ceilingfish
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
没有办法完全禁用垃圾收集。垃圾收集仅在 JVM 空间不足时运行,因此您可以为程序提供更多内存。将这些命令行选项添加到 Java 命令
-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 profileris very useful for figuring out what is taking a long time.
这为程序提供了 256Mb 的内存(默认为 64Mb)。不过,对于默认大小的 JVM,垃圾收集不会花费 3 秒,因此您可能需要更仔细地调查程序在做什么。Yourkit 分析器对于找出需要很长时间的内容非常有用。
回答by unbeli
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.
GC 仅在 JVM 内存不足时才会启动,因此您要么 GC 要么死。尝试打开详细 GC,看看它是否真的需要大量时间。
java -verbose:gc
回答by Mark
Are you sure that it is garbage collection causing the slowdown? Have you run java with -verbose:gc to see what is happening?
您确定是垃圾收集导致速度变慢吗?您是否使用 -verbose:gc 运行 java 以查看发生了什么?
You cannot disable garbage collection on the JVM. You could however look at tuningthe garbage collector for better performance.
您不能在 JVM 上禁用垃圾收集。但是,您可以考虑调整垃圾收集器以获得更好的性能。
回答by Michael Aaron Safyan
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.
您可以使用该-Xmx
选项来设置最大堆大小;使用更大的堆应该可以防止 VM 内存不足,从而很快就需要垃圾收集。
回答by Paul Jowett
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.
听起来您正在努力节省时间,但却以错误的方式进行。与启动和关闭 java 进程所花费的时间相比,禁用垃圾收集所节省的时间将是微不足道的(对于单个任务)。如果运行时性能是您的目标,您可能需要考虑启动一个 Java 进程,您可以多次要求该进程执行所需的工作。
回答by display101
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.
正如大家所说,您不能在 JVM 中禁用 GC,这是有道理的,因为如果可以的话,由于 Java 没有明确的方式让开发人员删除堆数据,因此会出现内存泄漏。
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.
您是否可以访问此 java 二进制文件的源代码?如果是这样,可能值得对其进行分析并查看是否存在可以更好地编写以减少 GC 活动的瓶颈。这可以通过大多数 Java 分析器来完成,例如 JProbe。
回答by Rodrigo Hernandez
To avoid garbage collector release a variable or property from any object, you must set this property (released by gc) as static in your class it was my solution.
为了避免垃圾收集器从任何对象中释放变量或属性,您必须在类中将此属性(由 gc 释放)设置为静态,这是我的解决方案。
Example:
例子:
private static String myProperty;
私有静态字符串 myProperty;
回答by aventurin
Java 11 comes with an no-op garbage collector.
Java 11 带有一个无操作垃圾收集器。
It can be enabled by the -XX:+UseEpsilonGC
option at JVM start.
它可以通过-XX:+UseEpsilonGC
JVM 启动时的选项启用。
According to the JEP decriptionone of its goals is to make certain short-lived jobs more efficient, what might be you use case:
根据JEP 的描述,其目标之一是使某些短期工作更有效率,您可能会使用什么用例:
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.
极短命的工作。短期作业可能依赖于快速退出以释放资源(例如堆内存)。在这种情况下,接受 GC 循环以徒劳地清理堆是浪费时间,因为无论如何堆都会在退出时被释放。请注意,GC 周期可能需要一段时间,因为它取决于堆中的实时数据量,这可能很多。