java 8 在运行时找出元空间的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31010463/
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
java 8 find out size of metaspace at runtime
提问by riship89
Java 8 uses metaspace that is capable of expanding dynamically. GC will run on metaspace when its getting full. Does that mean GC will never run on metaspace?
Java 8 使用能够动态扩展的元空间。当元空间变满时,GC 将在元空间上运行。这是否意味着 GC 永远不会在元空间上运行?
My Java 8 application is using a LOT of memory. I would like to know what is the size of my metaspace at the run time. How do I do that?
我的 Java 8 应用程序使用了大量内存。我想知道运行时我的元空间的大小是多少。我怎么做?
I am thinking of setting MaxMetaspaceSize
. What should I set it to? Any recommendations?
我正在考虑设置MaxMetaspaceSize
。我应该把它设置成什么?有什么建议吗?
回答by geld0r
Option 1:
选项1:
Execute
执行
jstat -gc PID
(with PID replaced by the PID of the JVM to monitor) which will return sth. like:
(将 PID 替换为要监控的 JVM 的 PID)这将返回 sth。喜欢:
S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
103936.0 107008.0 0.0 41618.3 820736.0 401794.3 444928.0 183545.7 181888.0 137262.8 28544.0 20386.6 313 16.024 8 3.706 19.729
According to thisof interest are:
根据这个兴趣是:
MC: Metaspace capacity (kB)
MU: Metaspace utilization (kB)
So in this case about 181mb of Metaspace are committed while 137mb are used currently.
所以在这种情况下,大约 181mb 的 Metaspace 被提交,而当前使用了 137mb。
Option 2:
选项 2:
If you have garbage collection logs enabled you can also find this out from there, e.g. after the application crashed already or an issue was reported. Search for lines like
如果您启用了垃圾收集日志,您也可以从那里找到它,例如在应用程序已经崩溃或报告了问题之后。搜索类似的行
2016-04-06T01:50:04.842+0200: 7.795: [Full GC (Metadata GC Threshold)
[PSYoungGen: 7139K->0K(177152K)]
[ParOldGen: 18396K->22213K(101888K)] 25535K->22213K(279040K),
[Metaspace: 34904K->34904K(1081344K)], 0.1408610 secs]
[Times: user=0.45 sys=0.00, real=0.14 secs]
This constitutes a resizing of the Metaspace as the previous threshold was reached.
这构成了在达到前一个阈值时调整元空间的大小。
[Metaspace: 34904K->34904K(1081344K)], 0.1408610 secs]
contains the relevant information: 34,9mb were used before as well as after GC. The latest of these log entries you can find will show the current size (that is: after GC).
包含相关信息:在 GC 之前和之后使用了 34,9mb。您可以找到的最新日志条目将显示当前大小(即:在 GC 之后)。
Keep in mind that full GC is run whenever Metaspace is resized. So its a good idea to configure a good start value for this when you already know that the default value of ~21mb (depending on Host configuration) is not enough.
请记住,只要调整 Metaspace 的大小,就会运行 full GC。因此,当您已经知道 ~21mb(取决于主机配置)的默认值是不够的时,最好为此配置一个好的起始值。
See thisfor more information on tuning Metaspace size.
请参阅此有关调整大小元空间的更多信息。
回答by Jigar Joshi
for (MemoryPoolMXBean memoryMXBean : ManagementFactory.getMemoryPoolMXBeans()) {
if ("Metaspace".equals(memoryMXBean.getName())) {
System.out.println(memoryMXBean.getUsage().getUsed()/1024/1024 + " mb");
}
}
Yes, it is better to set MaxMetaspaceSize
if you think it is leaky to monitor and fix it.
是的,MaxMetaspaceSize
如果您认为监控和修复它有漏洞,最好进行设置。
回答by Jim Chertkov
Use jcmd. It's the best built-in oracle tool for finding this stuff in Java 8. From terminal...
使用 jcmd。它是在 Java 8 中找到这些东西的最好的内置 oracle 工具。从终端......
NOTE: you need to run jcmd as whatever user started the java process. If you are debugging a webserver like Jetty you may need to change user with something like sudo su jetty
.
注意:您需要以任何用户启动 java 进程的身份运行 jcmd。如果您正在调试像 Jetty 这样的网络服务器,您可能需要使用类似sudo su jetty
.