前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊jvm的mapped buffer的统计

聊聊jvm的mapped buffer的统计

原创
作者头像
code4it
发布2024-01-07 11:51:33
1860
发布2024-01-07 11:51:33
举报
文章被收录于专栏:码匠的流水账

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

示例

代码语言:javascript
复制
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

代码语言:javascript
复制
{
    "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

代码语言:javascript
复制
{
    "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

代码语言:javascript
复制
{
    "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

开启

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

监控

代码语言:javascript
复制
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貌似也没办法统计到。

小结

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 示例
  • jvm.buffer.memory.used
    • mapped
      • direct
      • Native Memory Tracking
        • 开启
          • 监控
          • 小结
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档