Java -Xms 初始大小效果

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

Java -Xms initial size effects

javamemorygarbage-collectionheap

提问by SyBer

What is the benefit of setting the -Xms parameter, and having the initial memory larger for example, then the default calculated one (64 MB in my case, according to Java GC tunning: http://java.sun.com/javase/technologies/hotspot/gc/gc_tuning_6.html#par_gc.ergonomics.default_size)?

设置 -Xms 参数有什么好处,例如将初始内存更大,然后默认计算一个(在我的情况下为 64 MB,根据 Java GC 调整:http: //java.sun.com/javase/ technology/hotspot/gc/gc_tuning_6.html#par_gc.ergonomics.default_size)?

Also, is there any good to setting both the initial and maximum memories to same size?

另外,将初始和最大内存设置为相同大小有什么好处吗?

Thanks.

谢谢。

采纳答案by Phil

The benefit is that there is a performance penalty when you use up enough of the heap that it has to be resized. If you set it initially to 64MB but it turns out your application under load needs 250MB, when you hit near 64MB the JVM will allocate more heap space and possibly move around some objects and do other book-keeping. This of course takes time.

好处是当您使用足够的堆以调整其大小时会降低性能。如果您最初将其设置为 64MB,但结果表明负载下的应用程序需要 250MB,当您达到接近 64MB 时,JVM 将分配更多的堆空间并可能移动一些对象并进行其他簿记。这当然需要时间。

When your application is under load, you want all resources dedicated to making it run, so this extra work can make the application slower to respond, or even in some instances it can crash if it runs out of memory before the heap is resized.

当您的应用程序负载不足时,您希望所有资源专用于使其运行,因此这项额外的工作会使应用程序响应变慢,甚至在某些情况下,如果在调整堆大小之前内存不足,它可能会崩溃。

Sometimes when using a Java app, you'll see instructions like "set Xmsand Xmxto the same value". This is done to avoid the resizing altogether, so that your application launches with its heap already as big as it will ever be.

有时在使用 Java 应用程序时,您会看到诸如“设置Xms并设置Xmx为相同值”之类的说明。这样做是为了完全避免调整大小,以便您的应用程序启动时其堆已经和以往一样大。

回答by Roman

The linked article explains it clear enough:

链接的文章解释得足够清楚:

Default values: -Xms 3670k -Xmx 64m [...] Large server applications often experience two problems with these defaults. One is slow startup, because the initial heap is small and must be resized over many major collections. A more pressing problem is that the default maximum heap size is unreasonably small for most server applications. The rules of thumb for server applications are:

默认值:-Xms 3670k -Xmx 64m [...] 大型服务器应用程序经常遇到这些默认值的两个问题。一种是启动缓慢,因为初始堆很小并且必须在许多主要集合上调整大小。一个更紧迫的问题是,对于大多数服务器应用程序来说,默认的最大堆大小小得不合理。服务器应用程序的经验法则是:

  1. Unless you have problems with pauses, try granting as much memory as possible to the virtual machine. The default size (64MB) is often too small.
  2. Setting -Xms and -Xmx to the same value increases predictability by removing the most important sizing decision from the virtual machine. However, the virtual machine is then unable to compensate if you make a poor choice.
  3. In general, increase the memory as you increase the number of processors, since allocation can be parallelized.
  1. 除非您遇到暂停问题,否则请尝试为虚拟机授予尽可能多的内存。默认大小 (64MB) 通常太小。
  2. 将 -Xms 和 -Xmx 设置为相同的值可以通过从虚拟机中删除最重要的大小决定来提高可预测性。但是,如果您做出了错误的选择,虚拟机将无法进行补偿。
  3. 通常,随着处理器数量的增加而增加内存,因为分配可以并行化。

You can also be interested in this discussionof the problem.

您也可以对这个问题的讨论感兴趣。

回答by Tomislav Nakic-Alfirevic

If it is normal for your application to require more than 64 MB of heap memory, setting Xms to a larger value should improve the application's performance somewhat because the VM would not have to request additional memory as many times.

如果您的应用程序需要超过 64 MB 的堆内存是正常的,那么将 Xms 设置为更大的值应该会在一定程度上提高应用程序的性能,因为 VM 不必多次请求额外的内存。

In a production system I consider setting Xms and Xmx to the same value sensible. It's basically saying "this is the amount of heap memory the VM can get and I'm dedicating it right away".

在生产系统中,我考虑将 Xms 和 Xmx 设置为相同的值。它基本上是在说“这是 VM 可以获得的堆内存量,我马上就将其投入使用”。

回答by Pascal Thivent

What is the benefit of setting the -Xms parameter, and having the initial memory larger for example, then the default calculated one

设置 -Xms 参数有什么好处,例如初始内存更大,然后默认计算一个

If the initial heap is small and must be resized over many major collections, the startup will be slow.

如果初始堆很小并且必须在许多主要集合上调整大小,则启动会很慢。

Also, is there any good to setting both the initial and maximum memories to same size?

另外,将初始和最大内存设置为相同大小有什么好处吗?

Setting -Xmsand -Xmxto the same value gives you predictability. This is especially important when sizing the JVM during performance tuning. But the JVM won't be able to compensate any bad decision.

-Xms和设置-Xmx为相同的值可为您提供可预测性。这在性能调整期间调整 JVM 大小时尤其重要。但是 JVM 将无法补偿任何错误的决定。

I tend to use the same values for production servers (which are tuned during performance testing).

我倾向于对生产服务器使用相同的值(在性能测试期间进行调整)。