java java堆空间是由RAM还是硬盘决定的?

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

Is java heap space dictated by RAM or hard drive?

javamemory

提问by BitAndBytes

I'm performing a huge calculation and I'm getting a "out of heap space" error. What I'm wondering is if Java does paging automatically, so that maybe my hard drive space can be used when performing this calculation? If it doesn't do it automatically, how do I turn this option on?

我正在执行一个巨大的计算,我收到了一个“堆空间不足”的错误。我想知道的是 Java 是否会自动进行分页,以便在执行此计算时可以使用我的硬盘空间?如果它没有自动执行,我该如何打开这个选项?

回答by Matt Ball

The Java heap lives in RAM (ignoring virtual memory :). You can change the default initial heap size and maximum heap size with the -Xmsand -XmxVM args, respectively.

Java 堆位于 RAM 中(忽略虚拟内存:)。您可以分别使用-Xms-XmxVM args更改默认初始堆大小和最大堆大小。

The old generation, default heap size can be overridden by using the -Xms and -Xmx switches to specify the initial and maximum sizes respectively:

java -Xms <initial size> -Xmx <maximum size> program

For example:

java -Xms128m -Xmx512m application

可以通过使用 -Xms 和 -Xmx 开关分别指定初始和最大大小来覆盖老年代的默认堆大小:

java -Xms <initial size> -Xmx <maximum size> program

例如:

java -Xms128m -Xmx512m application


How much RAM does your machine have? Just what sort of a calculation is this that you think you need more heap than that?

你的机器有多少内存?这是一种什么样的计算,你认为你需要比这更多的堆?

回答by Derek Mahar

The virtual memory manager of the operating system, not the Java Virtual Machine (JVM), decides when to eject memory pages and write them to the hard disk.

操作系统的虚拟内存管理器,而不是 Java 虚拟机 (JVM),决定何时弹出内存页面并将它们写入硬盘。

As earlier answers have mentioned, the JVM stores the heap space entirely in resident memory and most JVM implementations allow the user to specify the maximum heap space. For example, the OpenJDKallows you to set the maximum heap size using the -Xmx<size>option. Run java -Xto see this and other non-standard OpenJDK command-line options.

正如前面的答案所提到的,JVM 将堆空间完全存储在常驻内存中,并且大多数 JVM 实现允许用户指定最大堆空间。例如,OpenJDK允许您使用该-Xmx<size>选项设置最大堆大小。运行java -X以查看此选项和其他非标准 OpenJDK 命令行选项。

回答by Jon Skeet

The size of the heap can determined however the JVM wants to. For most, it's via the -Xmx command line argument, with varying defaults. Chances are you want something like:

堆的大小可以由 JVM 决定。大多数情况下,它是通过 -Xmx 命令行参数,具有不同的默认值。你可能想要这样的东西:

java -Xmx2048M foo.bar.MyProgram

回答by Mike Samuel

It's determined by the operating system and by special flags.

它由操作系统和特殊标志决定。

$ java -X
...
    -Xms<size>        set initial Java heap size
    -Xmx<size>        set maximum Java heap size
    -Xss<size>        set java thread stack size
...