Linux 进程内存与堆——JVM

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

Process Memory Vs Heap -- JVM

javalinuxtomcat

提问by user546352

We have a web application deployed on a tomcatserver. There are certain scheduled jobs which we run, after which the heap memory peaks up and settles down, everything seems fine. However the system admin is complaining that memory usage ('top' on Linux ) keeps increasing the more the scheduled jobs are. Whats the co-relation between heap memory and memory of the CPU? Can it be controlled by any JVM settings? I used JConsoleto monitor the system.
I forced the garbage collection through JConsole and the heap usage came down, however the memory usage on Linuxremained high and it never decreased.

我们在tomcat服务器上部署了一个 Web 应用程序。我们运行了某些预定的作业,之后堆内存达到峰值并稳定下来,一切似乎都很好。然而,系统管理员抱怨内存使用量(Linux 上的“top”)随着预定作业的增加而不断增加。CPU 的堆内存和内存之间的相互关系是什么?它可以由任何 JVM 设置控制吗?我使用JConsole来监控系统。
我通过 JConsole 强制进行垃圾回收,堆使用率下降了,但是Linux上的内存使用率仍然很高,而且从未减少过。

Any ideas or suggestions would of great help?

任何想法或建议都会有很大帮助?

采纳答案by Jé Queue

What is likely being observed is the virtual size and not the resident set size of the Java process(es)? If you have a goal for a small footprint, you may want to notinclude -Xmsor any minimum size on the JVM heap arguments and adjust the 70% -XX:MaxHeapFreeRatio=to a smaller number to allow for more aggressive heap shrinkage.

可能观察到的是 Java 进程的虚拟大小而不是常驻集大小?如果你有一个小占位面积的目标,你可能想包含-Xms或在JVM堆参数中的任何最小尺寸和调整70%-XX:MaxHeapFreeRatio=为较小的数字,以便更积极的堆收缩。

In the meantime, provide more detail as to what was observed with the comment the Linux memory never decreased? What metric?

同时,请提供有关 Linux 内存从未减少的评论中观察到的更多细节?什么指标?

回答by wmacura

You can use -Xmx and -Xms settings to adjust the size of the heap. With tomcat you can set an environment variable before starting:

您可以使用 -Xmx 和 -Xms 设置来调整堆的大小。使用 tomcat,您可以在开始之前设置环境变量:

export JAVA_OPTS=”-Xms256m -Xmx512m”

This initially creates a heap of 256MB, with a max size of 512MB.

这最初会创建一个 256MB 的堆,最大大小为 512MB。

Some more details: http://confluence.atlassian.com/display/CONF25/Fix+'Out+of+Memory'+errors+by+increasing+available+memory

一些细节: http://confluence.atlassian.com/display/CONF25/Fix+“+内存已满] +” +错误+通过+增加+可用内存+

回答by Tim Bender

The memory allocated by the JVM process is not the same as the heap size. The used heap size could go down without an actual reduction in the space allocated by the JVM. The JVM has to receive a trigger indicating it should shrink the heap size. As @Xepoch mentions, this is controlled by -XX:MaxHeapFreeRatio.

JVM 进程分配的内存与堆大小不同。使用的堆大小可能会减少,而 JVM 分配的空间不会实际减少。JVM 必须接收一个触发器,指示它应该缩小堆大小。正如@Xepoch 提到的,这是由-XX:MaxHeapFreeRatio.

However the system admin is complaining that memory usage ('top' on Linux ) keeps increasing the more the scheduled jobs are [run].

然而,系统管理员抱怨内存使用量(Linux 上的“top”)随着预定作业 [run] 的增加而不断增加。

That's because you very likely have some sort of memory leak. System admins tend to complain when they see processes slowly chew up more and more space.

那是因为您很可能存在某种内存泄漏。当系统管理员看到进程慢慢占用越来越多的空间时,他们往往会抱怨。

Any ideas or suggestions would of great help?

任何想法或建议都会有很大帮助?

Have you looked at the number of threads? Is you application creating its own threads and sending them off to deadlock and wait idly forever? Are you integrating with any third party APIs which may be using JNI?

你看过线程数吗?您的应用程序是否创建了自己的线程并将它们发送到死锁并永远等待?您是否与任何可能使用 JNI 的第三方 API 集成?