Java JVM 堆参数

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

JVM heap parameters

javajvm-arguments

提问by Manuel Selva

After reading already asked question on the subject and a lot of googling I am still not able to have a clear view of -Xmsoption

在阅读了有关该主题的已问问题并进行了大量谷歌搜索后,我仍然无法清楚地了解-Xms选项

My question is: what's the difference between java -Xms=512m -Xmx=512mand java -Xms=64m -Xmx=512m?

我的问题是:java -Xms=512m -Xmx=512m和之间有什么区别java -Xms=64m -Xmx=512m

For now I have the following answer:

现在我有以下答案:

The only difference is in the number of garbage collections that will be run during my application's run and the number of memory allocations. Am I right ?

唯一的区别是在我的应用程序运行期间将运行的垃圾回收数量和内存分配数量。我对吗 ?

Here are my reasons for this answer:

以下是我给出这个答案的原因:

Setting the -Xmsoption to 512mdoesn't result in my application using really 512Mof physical memory after startup. I guess this is related to modern OS virtual memory management and lazy pages allocations. (I noticed that setting -Xmsto 512Mor to 64Mdoesn't change at all the initial used memory reported either by top on Linux or by the task manager on windows)

将该-Xms选项设置为512m不会导致我的应用程序512M在启动后真正使用物理内存。我想这与现代操作系统虚拟内存管理和延迟页面分配有关。(我注意到设置-Xmsto512M或 to64M根本不会改变由 Linux 上的 top 或 Windows 上的任务管理器报告的初始使用内存)

Can someone help me to understand the impact of this Xmsoption or point me to links that will help me to understand it?

有人可以帮助我了解此Xms选项的影响或指向我可以帮助我理解它的链接吗?

Thanks in advance

提前致谢

Manu

摩奴

采纳答案by Turismo

To summarize the information found after the link: The JVM allocates the amount specified by -Xms but the OS usually does not allocate real pages until they are needed. So the JVM allocates virtual memory as specified by Xms but only allocates physical memory as is needed.

总结链接后发现的信息:JVM 分配由 -Xms 指定的数量,但操作系统通常不会分配实际页面,直到需要它们。因此 JVM 会按照 Xms 的指定分配虚拟内存,但仅根据需要分配物理内存。

You can see this by using Process Explorer by Sysinternals instead of task manager on windows.

您可以通过使用 Sysinternals 的 Process Explorer 而不是 Windows 上的任务管理器来查看这一点。

So there is a real difference between using -Xms64M and -Xms512M. But I think the most important difference is the one you already pointed out: the garbage collector will run more often if you really need the 512MB but only started with 64MB.

因此,使用 -Xms64M 和 -Xms512M 之间存在真正的区别。但我认为最重要的区别是你已经指出的:如果你真的需要 512MB 但只从 64MB 开始,垃圾收集器会更频繁地运行。

回答by Tommaso Taruffi

if you wrote: -Xms512m -Xmx512m when it start, java allocate in those moment 512m of ram for his process and cant increment.

如果您写道: -Xms512m -Xmx512m 当它启动时,java 在那一刻为其进程分配 512m 的 ram 并且不能增加。

-Xms64m -Xmx512m when it start, java allocate only 64m of ram for his process, but java can be increment his memory occupation while 512m.

-Xms64m -Xmx512m 启动时,java 只为其进程分配 64m 的 ram,但是 java 可以在 512m 的同时增加他的内存占用。

I think that second thing is better because you give to java the automatic memory management.

我认为第二件事更好,因为您为 java 提供了自动内存管理。

回答by Steve B.

The JVM will start with memory useage at the initial heap level. If the maxheap is higher, it will grow to the maxheap size as memory requirements exceed it's current memory.

JVM 将从初始堆级别的内存使用情况开始。如果 maxheap 更高,则随着内存需求超过其当前内存,它将增长到 maxheap 大小。

So,

所以,

  • -Xms512m -Xmx512m
  • -Xms512m -Xmx512m

JVM starts with 512 M, never resizes.

JVM 以 512 M 开头,从不调整大小。

  • -Xms64m -Xmx512m
  • -Xms64m -Xmx512m

JVM starts with 64M, grows (up to max ceiling of 512) if mem. requirements exceed 64.

JVM 以 64M 开始,如果 mem,则增长(最大上限为 512)。要求超过 64。

回答by bajafresh4life

The JVM resizes the heap adaptively, meaning it will attempt to find the best heap size for your application. -Xms and -Xmx simply specifies the range in which the JVM can operate and resize the heap. If -Xms and -Xmx are the same value, then the JVM's heap size will stay constant at that value.

JVM 自适应地调整堆大小,这意味着它将尝试为您的应用程序找到最佳堆大小。-Xms 和 -Xmx 只是指定 JVM 可以操作和调整堆大小的范围。如果 -Xms 和 -Xmx 是相同的值,那么 JVM 的堆大小将保持在该值不变。

It's typically best to just set -Xmx and let the JVM find the best heap size, unless there's a specific reason why you need to give the JVM a big heap at JVM launch.

通常最好只设置 -Xmx 并让 JVM 找到最佳堆大小,除非有特定原因需要在 JVM 启动时为 JVM 提供一个大堆。

As far as when the JVM actually requests the memory from the OS, I believe it depends on the platform and implementation of the JVM. I imagine that it wouldn't request the memory until your app actually needs it. -Xmx and -Xms just reserves the memory.

至于JVM实际何时向操作系统请求内存,我认为这取决于JVM的平台和实现。我想在您的应用程序真正需要它之前它不会请求内存。-Xmx 和 -Xms 只是保留内存。

回答by Seema Kiran

Apart from standard Heap parameters -Xmsand -Xmxit's also good to know -XX:PermSizeand -XX:MaxPermSize, which is used to specify size of Perm Gen space because even though you could have space in other generation in heap you can run out of memory if your perm gen space gets full. This link also has nice overview of some important JVM parameters.

除了标准的堆参数,-Xms并且-Xmx它也是很好的了解-XX:PermSize-XX:MaxPermSize,这是用来指定的烫发根空间的大小,因为即使你可能在堆在其他代空间,你可以在内存中运行了,如果你的烫发根空间已满。此链接还对一些重要的 JVM 参数进行了很好的概述。

回答by Akavall

I created this toy example in scala, my_file.scala:

我在scala, 中创建了这个玩具示例my_file.scala

object MyObject {

    def main(args: Array[String]) {
        var ab = ArrayBuffer.empty[Int]

        for (i <- 0 to 100 * 1000 * 1000) {
            ab += i
            if (i % 10000 == 0) {
                println("On : %s".format(i))
            }
        }
    }
}

I ran it with:

我运行它:

scala -J-Xms500m -J-Xmx7g my_file.scala

and

scala -J-Xms7g -J-Xmx7g my_file.scala

There are certainly noticeable pauses in -Xms500mversion. I am positive that the short pauses are garbage collection runs, and the long ones are heap allocations.

-Xms500m版本中肯定有明显的停顿。我肯定短暂停是垃圾收集运行,长暂停是堆分配。