监视Java本机内存 [英] Monitoring java native memory

查看:66
本文介绍了监视Java本机内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在监视堆,元空间,线程和gc计数之类的jvm度量标准,并且能够将这些度量标准推送到诸如prometheus之类的监视服务器上.同样,我们想跟踪Java本机内存指标(jcmd VM.sumary的输出). 我的问题是,是否可以通过调用任何jvm运行时类来获取这些指标?

We are monitoring jvm metrics like heap, metaspace, threads and gc count and we are able to push these metrics to monitorng server like prometheus. Similarly we wanted to track Java native memory metrics(output of jcmd VM.sumary). My question is, is it possible to get these metrics by calling any jvm runtime classes?

推荐答案

是的,可以直接从Java应用程序获取NativeMemoryTracking摘要:

Yes, it is possible to get NativeMemoryTracking summary directly from a Java application:

import javax.management.JMException;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;

public class DiagnosticCommand {

    public static String execute(String command, String... args) throws JMException {
        return (String) ManagementFactory.getPlatformMBeanServer().invoke(
                new ObjectName("com.sun.management:type=DiagnosticCommand"),
                command,
                new Object[]{args},
                new String[]{"[Ljava.lang.String;"});
    }

    public static void main(String[] args) throws Exception {
        String summary = DiagnosticCommand.execute("vmNativeMemory", "summary");
        System.out.println(summary);
    }
}

不过,您必须解析文本输出.

You'll have to parse the text output though.

请注意,可以使用指定的MBean分别跟踪NMT报告中最重要的部分,包括

Note that the most important parts of the NMT report can be tracked separately with the designated MBeans, including

  • Java堆
  • 代码缓存
  • 元空间
  • 压缩班级空间
  • 直接字节缓冲区和映射字节缓冲区

请参见 BufferPoolMXBean .

正如我在评论中告诉的那样,监视NMT输出在实践中并不总是有帮助的,因为它不能直接反映该进程使用的实际物理内存. NMT可以报告

As I told in the comments, monitoring NMT output is not always helpful in practice, since it does not directly reflect the actual physical memory used by the process. NMT can report much less memory than the actual usage, or it can also report more memory than the process consumes from the OS perspective.

由于NMT会丢失Java进程消耗的大量OS内存,因此监视进程的驻留集大小(RSS)也很有用.在Linux上,可以通过解析 /proc/[pid]/stat /proc/[pid]/status .

Since NMT can miss the large amount of OS memory consumed by a Java process, it's also useful to monitor the process' resident set size (RSS). On Linux this can be done by parsing /proc/[pid]/stat or /proc/[pid]/status.

这篇关于监视Java本机内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆