首页 > 解决方案 > java heap dump 显示的保留堆内存比预期的要少

问题描述

我正在检查我的应用程序的堆转储。该应用程序有一个java.util.concurrent.ConcurrentHashMapwhere 键类型是java.lang.String,值是一个带有byte[]字段的用户定义类。

我添加了 16000 个键,每个键 30 个字符,值的字节数组为 3000 字节。所以我预计并发 hashmap 的总体保留堆大小为 48MB (16000 * 3000)。

但是堆转储中的值是 24MB。当我进一步检查时,我发现有很多条目如下所示。这些条目的HashMap Node 的retained size 远小于key 和value 的retained size 之和。

如何理解这种差异?

Class Name                                                                             | Shallow Heap | Retained Heap
----------------------------------------------------------------------------------------------------------------------
[5490] java.util.concurrent.ConcurrentHashMap$Node @ 0x6c6a23910                       |           32 |           136
|- <class> class java.util.concurrent.ConcurrentHashMap$Node @ 0x6c71bc320 System Class|            0 |             0
|- key java.lang.String @ 0x6c6a23930  /data-node-3-10/parent2/child2                  |           24 |           104
|- val org.apache.zookeeper.server.DataNode @ 0x6c6a23998                              |           32 |         3,304
----------------------------------------------------------------------------------------------------------------------

PS:我手头的任务是找到系统配置和内存使用之间的关联。

编辑:对于许多其他条目,值如下。HashMap 节点的保留大小(3160)包含节点的键(128)+值(3000)的保留大小。但是对于某些条目(例如,如上所示),对于 key 和 value 的保留大小分别为 104 和 3304 的节点,它似乎不正确(136)。

Class Name                                                                             | Shallow Heap | Retained Heap
----------------------------------------------------------------------------------------------------------------------
[9706] java.util.concurrent.ConcurrentHashMap$Node @ 0x6c6a1ab98                       |           32 |         3,160
|- <class> class java.util.concurrent.ConcurrentHashMap$Node @ 0x6c71bc320 System Class|            0 |             0
|- key java.lang.String @ 0x6c6a1abb8  /data-node-0-10/parent1/child2/grandchild1      |           24 |           128
|- val org.apache.zookeeper.server.DataNode @ 0x6c6a1ac38                              |           32 |         3,000
----------------------------------------------------------------------------------------------------------------------

标签: java

解决方案


推荐阅读