简体   繁体   English

动态设置Java进程的最大堆大小

[英]Dynamically setting maximum heap size for java process

I have a Java program that is launched by a batch file with a line like this: 我有一个批处理文件启动的Java程序,其行如下所示:

javaw -Xms64m -Xmx1024m com.acme.MyProgram

However, on some computers the program will not launch and displays the following message: 但是,在某些计算机上,该程序将无法启动,并显示以下消息:

Could not reserve enough space for object heap. 无法为对象堆保留足够的空间。 Could not create the Java virtual machine. 无法创建Java虚拟机。

The problem seems to be the the maximum size of the memory allocation pool is larger than the computer can handle. 问题似乎是内存分配池的最大大小大于计算机可以处理的大小。 Reducing the maximum size of the memory allocation pool from 1024m to 512m seems to resolve the problem. 将内存分配池的最大大小从1024m减少到512m似乎可以解决此问题。

Is there a way I can determine how much memory is available on the computer ahead of time (from within the batch file) and determine whether to use -Xmx1024m or -Xmx512m in the batch file invocation? 有没有一种方法可以确定计算机上有多少可用的内存(从批处理文件中)并确定在批处理文件调用中使用-Xmx1024m还是-Xmx512m Note that this batch file only needs to work on Windows. 请注意,此批处理文件仅需要在Windows上运行。

Actually the Java VM already does something similar. 实际上,Java VM已经做了类似的事情。 If you do not specify -Xms or -Xmx , then these values are inferred from the amount of physical memory on the machine. 如果未指定-Xms-Xmx ,那么将从计算机上的物理内存量中推断出这些值。 Or at least so says this page . 或者至少说,这样这个页面

You could set -Xms to the minimum heap size which makes your application useful, and let Java determine a proper value for -Xmx . 您可以将-Xms设置为使应用程序有用的最小堆大小,并让Java为-Xmx确定合适的值。

您可以在此页面上查看一些答案: 让JVM根据需要增加内存需求,直至达到VM限制的大小?

If your program functions correctly with a max heap of 512m, I would use that value. 如果您的程序以最大512m的最大容量正确运行,我将使用该值。

That said I will also check to see if there is a way to do what you're asking as that is an interesting question. 也就是说,我还将检查是否有一种方法可以解决您的要求,因为这是一个有趣的问题。

You could execute from your batch file, check the error level on exit and restart at a lower memory if it failed. 您可以从批处理文件执行,检查退出时的错误级别,如果失败则从较低的内存重新启动。 I'm not sure the error level would work--if it doesn't you could also check how long it took the program to execute... any thing less than 10sec would be a giveaway. 我不确定错误级别是否会起作用-如果不是,您还可以检查该程序执行了多长时间...少于10秒的任何事情都是免费的。

Just a couple comments though-- 不过只有几个评论-

If you know it doesn't NEED more than 512, you should run a test to ensure that 1024 actually helps. 如果您知道它不需要超过512,则应运行测试以确保1024确实有帮助。 Larger heaps can often make your GC pauses longer and do little else. 较大的堆通常会使您的GC暂停时间更长,而无济于事。

If you're pretty sure you'll use a certain amount of ram (say, the heap will easily fill the 512 you are allocating), you should probably set the min to that number. 如果您确定要使用一定数量的内存(例如,堆将轻松填充正在分配的512),则应将最小值设置为该数量。 Setting both the min and max to 512 is good if your program allocates a bunch of stuff but is not situational (always uses about the same amount of ram) 如果您的程序分配了一堆东西,但不是根据情况(总是使用大约相同数量的内存),则将min和max都设置为512很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM