Java 尝试增加 jvm 内存时,您可以将 Xmx 设置为最大数字吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/461260/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:54:48  来源:igfitidea点击:

Is there a maximum number you can set Xmx to when trying to increase jvm memory?

javaeclipsejvm

提问by

Is there a max. size you can set Xmx to? I set it to 1024m and eclipse opens ok. When I set it above 1024, eclipse doesn't open and I get the error "jvm terminated. Exit code=-1"...

有没有上限 您可以将 Xmx 设置为大小?我将它设置为 1024m 并且 eclipse 打开正常。当我将其设置为 1024 以上时,eclipse 无法打开,并且出现错误“jvm 终止。退出代码=-1”...

I was doing this because I keep getting an "java.lang.OutOfMemoryError: Java heap space". I am reading in a 35.5Mb .txt file and this error occurs when it's just reading in the file using the "while((line = reader.readLine()) != null)" loop. I would have thought that 1024mb would have been enough. Can anyone help me?

我这样做是因为我不断收到“java.lang.OutOfMemoryError:Java 堆空间”。我正在读取一个 35.5Mb 的 .txt 文件,当它只是使用“ while((line = reader.readLine()) != null)”循环读取文件时会发生此错误。我会认为 1024mb 就足够了。谁能帮我?

回答by user54579

I think a 32 bit JVM has a maximum of 2GB memory.This might be out of date though. If I understood correctly, you set the -Xmx on Eclipse launcher. If you want to increase the memory for the program you run from Eclipse, you should define -Xmx in the "Run->Run configurations..."(select your class and open the Arguments tab put it in the VM arguments area) menu, and NOT on Eclipse startup

我认为 32 位 JVM 的最大内存为 2GB。不过这可能已经过时了。如果我理解正确,您可以在 Eclipse 启动器上设置 -Xmx。如果你想为你从 Eclipse 运行的程序增加内存,你应该在“运行->运行配置...”(选择你的类并打开参数选项卡把它放在虚拟机参数区域)菜单中定义 -Xmx , 而不是在 Eclipse 启动时

Edit: details you asked for. in Eclipse 3.4

编辑:您要求的详细信息。在 Eclipse 3.4 中

  1. Run->Run Configurations...

  2. if your class is not listed in the list on the left in the "Java Application" subtree, click on "New Launch configuration" in the upper left corner

  3. on the right, "Main" tab make sure the project and the class are the right ones

  4. select the "Arguments" tab on the right. this one has two text areas. one is for the program arguments that get in to the args[] array supplied to your main method. the other one is for the VM arguments. put into the one with the VM arguments(lower one iirc) the following:
    -Xmx2048m

    I think that 1024m should more than enough for what you need though!

  5. Click Apply, then Click Run

  6. Should work :)

  1. 运行->运行配置...

  2. 如果你的类没有出现在“Java Application”子树左侧的列表中,点击左上角的“New Launch configuration”

  3. 在右侧,“主要”选项卡确保项目和类是正确的

  4. 选择右侧的“参数”选项卡。这个有两个文本区域。一个是用于进入提供给 main 方法的 args[] 数组的程序参数。另一个用于 VM 参数。将以下内容放入带有 VM 参数(较低的 iirc)中:
    -Xmx2048m

    我认为 1024m 应该足以满足您的需求!

  5. 单击应用,然后单击运行

  6. 应该管用 :)

回答by kgiannakakis

Have a look at thisfor some common errors in setting the java heap. You've probably set the heap size to a larger value than your computer's physical memory.

看看这个设置java堆的一些常见错误。您可能已将堆大小设置为比计算机物理内存更大的值。

You should avoid solving this problem by increasing the heap size. Instead, you should profile your application to see where you spend such a large amount of memory.

您应该通过增加堆大小来避免解决此问题。相反,您应该分析您的应用程序以查看您在哪里花费了如此大量的内存。

回答by Pete Kirkham

Yes, there is a maximum, but it's system dependent. Try it and see, doubling until you hit a limit then searching down. At least with Sun JRE 1.6 on linux you get interesting if not always informative error messages (peregrino is netbook running 32 bit ubuntu with 2G RAM and no swap):

是的,有一个最大值,但它取决于系统。试试看,加倍直到达到极限,然后向下搜索。至少在 Linux 上的 Sun JRE 1.6 中,如果不总是提供信息性错误消息,您会很有趣(peregrino 是运行 32 位 ubuntu 的上网本,具有 2G RAM 且无交换):

peregrino:$ java -Xmx4096M -cp bin WheelPrimes 
Invalid maximum heap size: -Xmx4096M
The specified size exceeds the maximum representable size.
Could not create the Java virtual machine.

peregrino:$ java -Xmx4095M -cp bin WheelPrimes 
Error occurred during initialization of VM
Incompatible minimum and maximum heap sizes specified

peregrino:$ java -Xmx4092M -cp bin WheelPrimes 
Error occurred during initialization of VM
The size of the object heap + VM data exceeds the maximum representable size

peregrino:$ java -Xmx4000M -cp bin WheelPrimes 
Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.

(experiment reducing from 4000M until)

peregrino:$ java -Xmx2686M -cp bin WheelPrimes 
(normal execution)

Most are self explanatory, except -Xmx4095M which is rather odd (maybe a signed/unsigned comparison?), and that it claims to reserve 2686M on a 2GB machine with no swap. But it does hint that the maximum size is 4G not 2G for a 32 bit VM, if the OS allows you to address that much.

大多数都是不言自明的,除了 -Xmx4095M 很奇怪(可能是有符号/无符号比较?),并且它声称在没有交换的 2GB 机器上保留 2686M。但它确实暗示了 32 位 VM 的最大大小是 4G 而不是 2G,如果操作系统允许您处理那么多。

回答by Stephen Price

I think that it's around 2GB. While the answer by Pete Kirkham is very interesting and probably holds truth, I have allocated upwards of 3GB without error, however it did not use 3GB in practice. That might explain why you were able to allocate 2.5 GB on 2GB RAM with no swap space. In practice, it wasn't using 2.5GB.

我认为它大约是2GB。虽然 Pete Kirkham 的回答非常有趣并且可能是正确的,但我已经没有错误地分配了 3GB 以上的空间,但是它在实践中并没有使用 3GB。这或许可以解释为什么您能够在没有交换空间的情况下在 2GB RAM 上分配 2.5GB。实际上,它没有使用 2.5GB。