我需要监控JVM Metaspace的使用情况。你能帮我解决一下这件事吗?
如何找到使用的元空间大小?
使用以下命令,我找到了maxmetaspace和min metaspace:
AutoMapper.Mapper.CreateMap<OuterSrc, Destination>()
.ForMember(dest => dest.Title,
opts => opts.MapFrom(src => src.Source.Title));
MetaspaceSize = 21807104(20.796875MB)
MaxMetaspaceSize = 1073741824(1024.0MB)
但我如何才能找到现在使用的内存价值?
答案 0 :(得分:2)
我用
jstat -gcutil <pid> | awk '{print($5)}'
将Metaspace利用率打印为空间当前容量的百分比。
jstat还有其他选项,在此解释:
http://docs.oracle.com/javase/8/docs/technotes/tools/unix/jstat.html
答案 1 :(得分:1)
您可以使用MemoryPoolMXBean
。
List<MemoryPoolMXBean> memPool = ManagementFactory.getMemoryPoolMXBeans();
for (MemoryPoolMXBean p : memPool)
{
if ("Metaspace".equals(p.getName())
{
... here you have the memory pool mx bean with information about he metaspace
}
}
答案 2 :(得分:0)
从 Java 11 开始,jcmd 实用程序附带了一个用于显示详细元空间信息的新命令。
jcmd <PID> VM.metaspace
您可以在 Thomas Stuefe 的 this post 中找到深入的解释。
或者,您也可以使用 JDK Flight Recorder (JFR) 进行元空间监控,请参阅 JMC 中的“垃圾收集”视图和/或事件浏览器中的“元空间摘要”事件。
答案 3 :(得分:0)
如果您使用的是 JDK 16,您可以编写一个简单的程序来使用新的 Remote streaming API 来监控您的应用程序。首先在要监控的主机上启动JMX
$ jcmd <pid> ManagementAgent.start jmxremote.port=7091
jmxremote.authenticate=false jmxremote.ssl=false
然后使用以下程序,该程序会将所有元空间事件打印到标准输出。
$ java MetaspaceMonitor.java localhost 7091
源代码:
import java.io.IOException;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import jdk.management.jfr.RemoteRecordingStream;
public class MetaspaceMonitor {
public static void main(String... args) throws IOException {
String host = args[0];
Strin port = args[1];
String url = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi";
JMXServiceURL u = new JMXServiceURL(url);
JMXConnector c = JMXConnectorFactory.connect(u);
MBeanServerConnection connection = c.getMBeanServerConnection();
try (var s = new RemoteRecordingStream(connection)) {
s.enable("jdk.MetaspaceSummary");
s.enable("jdk.MetaspaceGCThreshold");
s.enable("jdk.MetaspaceAllocationFailure").withStackTrace();
s.enable("jdk.MetaspaceOOM").withStackTrace();
s.onEvent("jdk.MetaspaceSummary", System.out::println);
s.onEvent("jdk.MetaspaceGCThreshold", System.out::println);
s.onEvent("jdk.MetaspaceAllocationFailure", System.out::println);
s.onEvent("jdk.MetaspaceOOM", System.out::println);
s.start();
}
}
}