Java 堆内存是否根据 RAM 变化?

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

Does Java heap memory change based on RAM?

javamemory-management

提问by Lemon Juice

I know the default Java heap memory is 128 MB. Since it is the default, I want to know whether this memory get automaticallychanged according to the RAM size. For an example, for a machine with 128 MB RAM, heap memory 128 is too much and it should be automatically changed. Because if an application use all of 128 heap, PC will end up in a trouble.

我知道默认的 Java 堆内存是 128 MB。由于它是默认的,我想知道这个内存是否会根据 RAM 大小自动更改。举个例子,对于128MB RAM的机器,堆内存128太多了,应该自动改。因为如果一个应用程序使用所有 128 个堆,PC 最终会遇到麻烦。

please help.

请帮忙。

回答by Joshua McKinnon

In Java 1.6 update 18 (and later), if unspecified, the default heap-size in a client JVM follows these rules:

在 Java 1.6 update 18(及更高版本)中,如果未指定,客户端 JVM 中的默认堆大小遵循以下规则:

The default maximum heap size is half of the physical memory up to a physical memory size of 192 megabytes and otherwise one fourth of the physical memory up to a physical memory size of 1 gigabyte.

For example, if your machine has 128 megabytes of physical memory, then the maximum heap size is 64 megabytes, and greater than or equal to 1 gigabyte of physical memory results in a maximum heap size of 256 megabytes.

默认的最大堆大小是物理内存的一半,最大为 192 MB 的物理内存大小,否则为物理内存的四分之一,最大为 1 GB 的物理内存大小。

例如,如果您的机器有 128 兆字节的物理内存,则最大堆大小为 64 兆字节,大于或等于 1 GB 的物理内存导致最大堆大小为 256 兆字节。

Taken from the 1.6.0_18 update notes

取自1.6.0_18 更新说明

In previous releases of Java heap size was NOT variable by default.

在以前的 Java 版本中,默认情况下堆大小不是可变的。

回答by duffymo

No, it won't be automatically changed unless you ask for it on startup using the -Xmxoption.

不,除非您在启动时使用该-Xmx选项要求它,否则它不会自动更改。

You can't get more than 2 GB on a 32 bit machine in any case. If you need more than that, you'll have to get a 64 bit operating system with lots of RAM.

在任何情况下,在 32 位机器上都不能获得超过 2 GB 的空间。如果您需要更多,则必须获得具有大量 RAM 的 64 位操作系统。

回答by Subir Kumar Sao

You can specify JVM heap size by using aptions -Xmx and -Xms

您可以使用 aptions -Xmx 和 -Xms 指定 JVM 堆大小

-Xmx - Max Size. -Xms - Min Size.

-Xmx - 最大尺寸。-Xms - 最小尺寸。

You specify like

你指定像

 -Xmx64m -Xms32m  (for 64 and 32 MB)

回答by Rohan Grover

No. It is not automatically changed. Look herefor more on HotSpot JVM memory options .

不会。它不会自动更改。看看这里为更多的HotSpot JVM内存选项。

More Hereon handling Java OutOfMemoryError.

更多在这里上处理Java的OutOfMemoryError。