简体   繁体   English

如何从 Java 堆转储中提取 HashMap

[英]How to extract a HashMap from a Java heap dump

I have a Java heap dump generated using jmap.我有一个使用 jmap 生成的 Java 堆转储。 This contains a HashMap which I need to extract into a text format (CSV would be fine).这包含一个 HashMap,我需要将其提取为文本格式(CSV 就可以)。 The HashMap is fairly large so I need a scripted solution. HashMap 相当大,所以我需要一个脚本解决方案。

Using JVisualVM I can find the HashMap. However, there doesn't seem to be a way to export its data.使用 JVisualVM 我可以找到 HashMap。但是,似乎没有办法导出它的数据。 After some experimentation I did work out this OQL query:经过一些实验后,我确实计算出了这个 OQL 查询:

select map(filter(heap.findObject("0x12345678"), 'it != null'), function(it) { return {"id": it.key.value, "value": it.value.value}; })

Where 0x12345678 is the object ID of the table array within the HashMap. Even this doesn't quite work, as it only finds objects directly attached to the table, not chained objects.其中 0x12345678 是 HashMap 中表数组的 object ID。即使这样也不太有效,因为它只能找到直接附加到表的对象,而不是链接的对象。 In any case, I feel I'm making this harder than it has to be - I'd have thought there is a simple way to do this.无论如何,我觉得我让这件事变得比它必须的更难——我本以为有一种简单的方法可以做到这一点。

To extend geert3 answer:扩展 geert3 答案:

You can do it by using Memory Analyzer .您可以使用Memory Analyzer来完成。 Select "dominator_tree" and search your HashMap. Select“dominator_tree”并搜索您的 HashMap。

Select -> Right click -> Java Collections -> Hash Entries Select -> 右键单击 -> Java Collections -> Hash 条目

内存分析器截图

Apparently there is also an Eclipse tool (standalone as well as plugin): Memory Analyzer (MAT), maybe that offers more exporting options. 显然还有一个Eclipse工具(独立和插件):Memory Analyzer(MAT),可能提供更多的导出选项。 See http://www.eclipse.org/mat http://www.eclipse.org/mat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM