简体   繁体   English

JVM选项:Xmx分配

[英]JVM Option: Xmx Allocation

Although, the question is very basic, I am willing to understand how JVM's maximum memory allocation is done to an application. 虽然问题非常基础,但我愿意理解JVM如何对应用程序进行最大内存分配。 I have an application running on a Windows 2008 server that hosts about 60 virtual managed servers [that is 60 JVMs]. 我有一个在Windows 2008服务器上运行的应用程序,它承载大约60个虚拟托管服务器[即60个JVM]。 Each managed server is set a maximum heap of 1024m. 每个受管服务器的最大堆数设置为1024m。 The Windows is configured with a 32 GB RAM. Windows配置了32 GB RAM。

Now the question, how is maximum memory allocation to a JVM done? 现在的问题是,如何最大限度地为JVM分配内存? Is it done at one go or on a incremental growth basis? 它是一次完成还是逐步增长? If at one go, how does Windows handle all 60 managed servers hosted my application in a 32-GB-RAM-packed system? 如果一气呵成,Windows如何处理在32 GB RAM封装系统中托管我的应用程序的所有60个托管服务器?

Any views are much appreciated. 任何意见都非常感谢。 Thank you. 谢谢。

The JVM actually does allocate all memory requested by -Xmx at startup time, along with additional memory to hold the JVM executable and internal work areas. JVM实际上会在启动时分配-Xmx请求的所有内存,以及用于保存JVM可执行文件和内部工作区的额外内存。 And when you create threads, it also allocates memory for the stacks used for those threads. 当您创建线程时,它还为用于这些线程的堆栈分配内存。

This works because (1) the JVM doesn't actually use this memory, and (2) the OS provides a paging file. 这是因为(1)JVM实际上不使用此内存,(2)OS提供页面文件。 When the JVM requests an allocation, it creates a commitment by the OS but does not actually use all RAM requested. 当JVM请求分配时,它会由OS创建承诺,但实际上并不使用所有请求的RAM。 As it actually uses the RAM, the OS will swap pages to/from the pagefile. 由于它实际使用RAM,操作系统将页面与页面文件交换。 If all processes were actively using their RAM, the OS would have to swap pages in and out constantly; 如果所有进程都在积极使用其RAM,则操作系统必须不断地交换页面; this is called "thrashing." 这被称为“颠簸”。

The -Xms parameter specifies the initial heap size, within the overall heap size . -Xms参数指定整个堆大小内的初始堆大小 The JVM will attempt to keep memory within these bounds, but is permitted to expand the bounds if it cannot reclaim enough garbage. JVM将尝试将内存保留在这些边界内,但如果无法回收足够的垃圾,则允许扩展边界。 However, these heap increases do not incrementally request more memory from the OS. 但是,这些堆增加不会逐渐从OS请求更多内存。 If they did, the heap would be fragmented and a large array allocation might fail (because it wouldn't fit in contiguous memory). 如果他们这样做,堆将被分段并且大型数组分配可能会失败(因为它不适合连续的内存)。

JVM initially allocates amount of memory that is specified via -Xms option (default is 32M, as far as I know). JVM最初分配通过-Xms选项指定的内存量(据我所知,默认值为32M)。 When it runs out of this memory, it will start allocating new memory incrementally until it reaches the amount specified via -Xmx option (default is 64M). 当它耗尽此内存时,它将开始递增地分配新内存,直到达到通过-Xmx选项指定的数量(默认为64M)。

When this happens, OutOfMemory error is thrown. 发生这种情况时,会OutOfMemory错误。

PS Of course, this is a quite simplified algorithm. PS当然,这是一个非常简化的算法。 In fact, JVM uses a much more complex one. 实际上,JVM使用的是更复杂的一个。

如果您希望在启动进程时分配它,则需要提供-Xms,如果仅提供-Xmx,则VM将增长到该值,但只有在需要更多内存时才会增加。

-Xmx parameter specifies the maximum memory JVM can use, but it doesn't allocate it from the OS immediately after start-up. -Xmx参数指定JVM可以使用的最大内存,但它不会在启动后立即从操作系统中分配。 It does it incrementally , usually in many steps, as it needs to do so (ie. the Java application in JVM demands more memory as it runs). 它通常在许多步骤中递增地执行,因为它需要这样做(即,JVM中的Java应用程序在运行时需要更多内存)。

Of course this is the default behavior, it can be changed with many other JVM params. 当然这是默认行为,可以使用许多其他JVM参数进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM