java 为 JVM 启动内存分配

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

Starting memory allocation for JVM

javajvmjvm-arguments

提问by C. Ross

I'm beginning use the -Xmxoption on the javacommand to allow my processes to use a little more memory (256Mb, though I think I'm currently using less than 128Mb). I've also noticed the -Xmsoption for starting memory, with a default value of 2Mb. What should I set this value to and why?

我开始使用命令-Xmx上的选项java来允许我的进程使用更多的内存(256Mb,但我认为我目前使用的内存少于 128Mb)。我还注意到-Xms启动内存的选项,默认值为 2Mb。我应该将这个值设置为什么,为什么?



Reference: Java

参考:Java

回答by gmhk

The -Xmxargument defines the max memory size that the heap can reach for the JVM. You must know your program well and see how it performs under load and set this parameter accordingly. A low value can cause an OutOfMemoryExceptionor very poor performance if your program's heap memory is reaching the maximum heap size. If your program is running on a dedicated server you can set this parameter higher because it wont affect other programs.

-Xmx参数定义了堆可以为 JVM 达到的最大内存大小。您必须非常了解您的程序并查看它在负载下的执行情况并相应地设置此参数。OutOfMemoryException如果您的程序的堆内存达到最大堆大小,则较低的值可能会导致性能下降或非常差。如果您的程序在专用服务器上运行,您可以将此参数设置得更高,因为它不会影响其他程序。

The -Xmsargument sets the initial and minimum heap memory size for the JVM. This means that when you start your program the JVM will allocate this amount of memory instantly. This is useful if your program will consume a large amount of heap memory right from the start. This avoids the JVM needing to regularly increase the heap size and so you can gain some performance there. If you don't know if this parameter is going to help you, don't use it.

-Xms参数设置 JVM 的初始和最小堆内存大小。这意味着当您启动程序时,JVM 将立即分配此数量的内存。如果您的程序从一开始就消耗大量堆内存,这将非常有用。这避免了 JVM 需要定期增加堆大小,因此您可以获得一些性能。如果您不知道此参数是否对您有帮助,请不要使用它。

It is good practice with server-side Java applications like Resin to set the minimum -Xmsand maximum -Xmxheap sizes to the same value. You can set to 256 or 512Mb.

对于像 Resin 这样的服务器端 Java 应用程序,将最小-Xms和最大-Xmx堆大小设置为相同的值是一种很好的做法。您可以设置为 256 或 512Mb。

回答by Martyn

Heap size should be 'right' for your application finding right though isn't easy. If it's too small you run out of memory if to big you waste memory and will run the risk of having long GC pauses. When things go wrong (and they always go wrong) the more heap you have the more heap you have to debug and the longer any leaks will take to manifest themselves.

堆大小应该是“正确的”,让您的应用程序找到正确的方法虽然并不容易。如果它太小,你就会耗尽内存,如果太大,你会浪费内存,并且会有长时间 GC 暂停的风险。当出现问题时(而且总是出错),您拥有的堆越多,您需要调试的堆就越多,任何泄漏出现的时间就越长。

Ideally I would set -Xmsto be the size I think my application needs to run and -Xmxto a value larger than this (but not too large). Always switch on verbose garbage collection and graph the heap in use to check the values you have set.

理想情况下,我将设置-Xms为我认为我的应用程序需要运行的大小,并-Xmx设置为大于此的值(但不要太大)。始终打开详细垃圾收集并绘制正在使用的堆以检查您设置的值。

When I graph the heap I look at the number of collections occurring and the type. It's important not to have too many. However it's also important not to have a heap so huge that there are few, because when they happen (and full gc's are inevitable) they will hurt. Ideally a regular set of well spaced gcs with very low pause times is being sought.

当我绘制堆图形时,我会查看发生的集合数量和类型。重要的是不要有太多。然而,同样重要的是不要有一个大到很少的堆,因为当它们发生时(并且完全 gc 是不可避免的)它们会受到伤害。理想情况下,正在寻找具有非常低暂停时间的规则间隔良好的 gc。

That said for 32bit Windows -Xmsshould be the same as -Xmxthis is because Java (leaving aside jrockit) needs contiguous memory and the Windows' memory map is fragmented it therefore pays to get and lock down the heap as early as possible.

这对于 32 位 Windows 来说-Xms应该是一样的,-Xmx因为 Java(不考虑 jrockit)需要连续的内存,并且 Windows 的内存映射是碎片化的,因此尽早获取和锁定堆是值得的。

回答by Mike Thomsen

Xmx is the upper bound of the memory pool. Xms is the initial size. That's all there's to it. The appropriate size for each would depend on the complexity of your application. The main advantage of setting the initial size higher than 2mb would just be that the JVM would spend less time requesting more memory from the OS. If you've set an upper limit of 256mb, an initial size of 32mb-64mb wouldn't be unreasonable for most enterprise applications.

Xmx 是内存池的上限。Xms 是初始大小。这就是全部。每个的适当大小将取决于您的应用程序的复杂性。将初始大小设置为高于 2mb 的主要优点只是 JVM 将花费更少的时间从操作系统请求更多内存。如果您设置了 256mb 的上限,那么对于大多数企业应用程序来说,32mb-64mb 的初始大小不会不合理。

More here: http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html

更多信息:http: //java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html

回答by OscarRyz

What should I set this value to and why?

我应该将这个值设置为什么,为什么?

Depends on your application of course.

当然取决于你的应用程序。

If you know your application initially consumes up to, let's say 64 mb in the first minute, then using the default ( 2mb), will make the VM request memory several times until it reach this 64mb. These memory requests will slow down a little bit your application, because the garbage collector may run a number of times trying to free space before the VM ask for more.

如果您知道您的应用程序最初消耗多达 64 mb,假设在第一分钟内使用 64 mb,那么使用默认值 (2mb) 将使 VM 请求内存多次,直到达到此 64 mb。这些内存请求会稍微减慢您的应用程序的速度,因为垃圾收集器可能会在 VM 请求更多空间之前运行多次尝试释放空间。

If you know already that you'll use as start 64 mb, using -Xmsparameter will let you allocate that memory upfront.

如果您已经知道将使用 64 mb 作为起始-Xms内存,则 using参数将让您预先分配该内存。

If you're consuming 128mb and you have enough memory available you may use java -Xms128mand don't worry about requesting more memory for a long time.

如果您消耗 128mb 并且您有足够的可用内存,则可以使用java -Xms128m并且不用担心长时间请求更多内存。

But again, it depends on what your application does, how is the memory used, when is needed. etc. etc.

但同样,这取决于您的应用程序做什么,如何使用内存,何时需要。等等等等