聊聊jvm的mapped buffer的统计

本文主要研究一下jvm的mapped buffer的统计

示例

复制代码
private void writeDirectBuffer() {
    // 分配一个256MB的直接缓冲区
    ByteBuffer buffer = ByteBuffer.allocateDirect(256 * 1024 * 1024);

    // 填充数据
    Random random = new Random();
    while (buffer.remaining() >= 4) {
        buffer.putInt(random.nextInt());
    }

    System.out.println("Allocated direct buffer with capacity " + buffer.capacity());
}

private void writeMappedBuffer() throws IOException {
    RandomAccessFile file = new RandomAccessFile("/tmp/test.txt", "rw");
    FileChannel channel = file.getChannel();
    MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 128 * 1024 * 1024); // 映射128MB的空间
    // 随机写入数据
    for (int i = 0; i < buffer.capacity(); i++) {
        buffer.put((byte) i);
    }
}

jvm.buffer.memory.used

http://localhost:8080/actuator/metrics/jvm.buffer.memory.used

复制代码
{
    "name": "jvm.buffer.memory.used",
    "description": "An estimate of the memory that the Java virtual machine is using for this buffer pool",
    "baseUnit": "bytes",
    "measurements": [
        {
            "statistic": "VALUE",
            "value": 402685952
        }
    ],
    "availableTags": [
        {
            "tag": "id",
            "values": [
                "direct",
                "mapped"
            ]
        }
    ]
}

jvm.buffer.memory.used分了direct和mapped两大类,一共用了384MB

mapped

http://localhost:8080/actuator/metrics/jvm.buffer.memory.used?tag=id:mapped

复制代码
{
    "name": "jvm.buffer.memory.used",
    "description": "An estimate of the memory that the Java virtual machine is using for this buffer pool",
    "baseUnit": "bytes",
    "measurements": [
        {
            "statistic": "VALUE",
            "value": 134217728
        }
    ],
    "availableTags": []
}

可以看到这里mapped用了128MB

direct

http://localhost:8080/actuator/metrics/jvm.buffer.memory.used?tag=id:direct

复制代码
{
    "name": "jvm.buffer.memory.used",
    "description": "An estimate of the memory that the Java virtual machine is using for this buffer pool",
    "baseUnit": "bytes",
    "measurements": [
        {
            "statistic": "VALUE",
            "value": 268500992
        }
    ],
    "availableTags": []
}

可以看到这里direct用了256MB

Native Memory Tracking

开启

复制代码
java -XX:+UnlockDiagnosticVMOptions -XX:NativeMemoryTracking=summary -jar target/app.jar

监控

复制代码
jcmd 2315 VM.native_memory summary scale=MB
2315:

Native Memory Tracking:

Total: reserved=5882MB, committed=1100MB
-                 Java Heap (reserved=4096MB, committed=577MB)
                            (mmap: reserved=4096MB, committed=577MB)

-                     Class (reserved=1066MB, committed=46MB)
                            (classes #7035)
                            (malloc=10MB #10793)
                            (mmap: reserved=1056MB, committed=37MB)

-                    Thread (reserved=36MB, committed=36MB)
                            (thread #37)
                            (stack: reserved=36MB, committed=36MB)

-                      Code (reserved=246MB, committed=16MB)
                            (malloc=2MB #3971)
                            (mmap: reserved=244MB, committed=13MB)

-                        GC (reserved=160MB, committed=148MB)
                            (malloc=10MB #219)
                            (mmap: reserved=150MB, committed=138MB)

-                  Internal (reserved=266MB, committed=266MB)
                            (malloc=266MB #10067)

-                    Symbol (reserved=9MB, committed=9MB)
                            (malloc=8MB #74334)
                            (arena=2MB #1)

-    Native Memory Tracking (reserved=2MB, committed=2MB)
                            (tracking overhead=2MB)

NTM的Internal包含了direct buffer的统计,不包含mapped buffer的占用,而BufferPoolMXBean包含了direct和mapped。

另外通过调用System.loadLibrary()加载的共享库分配的内存NTM没办法追踪到,MXBean貌似也没办法统计到。

小结

相关推荐
源分享4 天前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm
JAVA9654 天前
JAVA面试-JVM篇 03-JVM运行时数据区哪些是线程私有的哪些是共享的
java·jvm·面试
伶俜665 天前
鸿蒙原生应用实战(十八)ArkUI 记账本:SQLite 账单 + 图表统计 + 分类管理
jvm·sqlite·harmonyos
IronMurphy5 天前
多线程问!
java·jvm·spring
unique5 天前
AI Coding 采集方案探索
jvm·人工智能·oracle
cfm_29146 天前
JVM GC日志解析
jvm
不良使6 天前
鸿蒙PC迁移:使用Electron`logseq-master-ohos` 鸿蒙适配全记录
jvm·electron·harmonyos
cfm_29146 天前
JVM深度详解:Class常量池、运行时常量池、字符串常量池、包装类对象池
java·jvm
JAVA9656 天前
JAVA面试-JVM篇 02-G1垃圾收集器的工作原理是什么与CMS的区别
java·jvm·面试