如何禁用Java垃圾收集器?

时间:2022-10-29 01:33:51

we have a PHP webapp that calls a java binary to produce a pdf report (with jasperreport), the java binary outpus pdf to standart output and exits, the php then send the pdf to browser. This java command lasts about 3 to 6 seconds, I think when it lasts 6 second it's because the GC kicks in, so I would like to disable it because anyway when the command exits all memory is returned..

我们有一个PHP webapp调用java二进制文件来生成pdf报告(使用jasperreport),java二进制文件输出pdf到标准输出并退出,然后php将pdf发送到浏览器。这个java命令持续大约3到6秒,我认为当它持续6秒时,因为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以查看哪个执行速度更快..

Thanks

8 个解决方案

#1


26  

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 profiler is very useful for figuring out what is taking a long time.

这给程序256Mb的ram(默认为64Mb)。但是,对于默认大小的JVM,垃圾收集不会花费3秒钟,因此您可能希望更密切地调查程序正在执行的操作。 Yourkit profiler对于弄清楚需要花费很长时间的内容非常有用。

#2


35  

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进程启动,如果您的目标是运行时性能,则可以多次请求执行所需的工作。

#3


26  

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.

当JVM内存不足时,GC才会启动,因此无论是GC还是死机。尝试打开详细GC并查看它是否确实需要大量时间。

java -verbose:gc

#4


2  

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运行内存不足,从而需要很快收集垃圾。

#5


1  

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 tuning the garbage collector for better performance.

您无法在JVM上禁用垃圾回收。但是,您可以查看调优垃圾收集器以获得更好的性能。

#6


1  

Java 11 comes with an no-op garbage collector.

Java 11附带了一个无操作的垃圾收集器。

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

它可以通过JVM启动时的-XX:+ UseEpsilonGC选项启用。

According to the JEP decription one 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周期可能需要一段时间,因为它取决于堆中的实时数据量,这可能很多。

#7


0  

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。

#8


0  

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;

private static String myProperty;

#1


26  

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 profiler is very useful for figuring out what is taking a long time.

这给程序256Mb的ram(默认为64Mb)。但是,对于默认大小的JVM,垃圾收集不会花费3秒钟,因此您可能希望更密切地调查程序正在执行的操作。 Yourkit profiler对于弄清楚需要花费很长时间的内容非常有用。

#2


35  

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进程启动,如果您的目标是运行时性能,则可以多次请求执行所需的工作。

#3


26  

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.

当JVM内存不足时,GC才会启动,因此无论是GC还是死机。尝试打开详细GC并查看它是否确实需要大量时间。

java -verbose:gc

#4


2  

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运行内存不足,从而需要很快收集垃圾。

#5


1  

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 tuning the garbage collector for better performance.

您无法在JVM上禁用垃圾回收。但是,您可以查看调优垃圾收集器以获得更好的性能。

#6


1  

Java 11 comes with an no-op garbage collector.

Java 11附带了一个无操作的垃圾收集器。

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

它可以通过JVM启动时的-XX:+ UseEpsilonGC选项启用。

According to the JEP decription one 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周期可能需要一段时间,因为它取决于堆中的实时数据量,这可能很多。

#7


0  

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。

#8


0  

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;

private static String myProperty;