简体   繁体   English

JVM 不分配使用 -Xmx 参数定义的所有 memory

[英]JVM does not allocate all memory that is defined with -Xmx argument

I have a jar file that i run with a systemd unit file.我有一个使用systemd单元文件运行的 jar 文件。 The run command in the unit file is the following:单元文件中的运行命令如下:

ExecStart=/usr/bin/java -Xms200m -Xmx465m --enable-preview -jar myapp-1.0.0.jar ExecStart=/usr/bin/java -Xms200m -Xmx465m --enable-preview -jar myapp-1.0.0.jar

My application always logs the maximum amount of RAM it is allowed to use.我的应用程序始终记录允许使用的最大 RAM 量。 It logs this using the following code:它使用以下代码记录这一点:

public static int ONE_MEGABYTE_IN_BYTES = 1048576;
Runtime runtime = Runtime.getRuntime();
int maximumJVMHeapAllocation = Math.round(runtime.maxMemory() / oneMegabyteInBytes);

For some reason the value of runtime.maxMemory() is always about 15mb less then the value of the -Xmx argument.由于某种原因, runtime.maxMemory()的值总是比-Xmx参数的值小15mb左右。 So if the jar file is run with the argument -Xmx465m then the application will only get 450mb of usable RAM memory.因此,如果使用参数-Xmx465m运行 jar 文件,则应用程序将仅获得450mb的可用 RAM memory。

My question is: What is the remaining 15 mb of RAM used for?我的问题是:剩余的 15 mb RAM有什么用? Is the used for stack memory?用于堆栈 memory 吗?

EDIT: To avoid confusion;编辑:为避免混淆; The total amount of RAM available to the server is 1GB .服务器可用的 RAM 总量为1GB 512MB of that is used by the operating system Amazon Linux 2 .其中512MB由操作系统Amazon Linux 2使用。

Apparently, JVM runs with Parallel or Serial GC, where the Young Generation of the heap consists of Eden and two Survivor spaces (see Generations ).显然,JVM 与并行或串行 GC 一起运行,其中堆的年轻代由 Eden 和两个幸存者空间组成(请参阅Generations )。

JVM keeps one of Survivor spaces empty: an application cannot use both simultaneously. JVM 将幸存者空间之一保持为空:应用程序不能同时使用两者。 Therefore, Runtime.maxMemory excludes the size of one Survivor space (which seems to be 15 MB in your case).因此, Runtime.maxMemory不包括一个 Survivor 空间的大小(在您的情况下似乎是 15 MB)。

Other GC algorithms behave differently.其他 GC 算法的行为不同。 For example, if you run JVM with -XX:+UseG1GC , the maximum available memory as shown by Runtime.maxMemory will be equal to -Xmx (more precisely, it'll be 466 MB because of the alignment ). For example, if you run JVM with -XX:+UseG1GC , the maximum available memory as shown by Runtime.maxMemory will be equal to -Xmx (more precisely, it'll be 466 MB because of the alignment ).

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

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