gpt4 book ai didi

java - 以编程方式分析java堆转储文件

转载 作者:行者123 更新时间:2023-12-02 04:54:04 24 4
gpt4 key购买 nike

我想编写一个程序(最好用java)来解析和分析java堆转储文件(由jmap创建)。我知道有很多很棒的工具已经可以这样做(jhat、eclipse 的 MAT 等),但我想从我的应用程序的特定角度来分析堆。

在哪里可以阅读有关堆转储文件的结构、如何读取它的示例等等?没有找到任何有用的搜索...

非常感谢。

最佳答案

以下内容是使用 Eclipse 版本完成的:Luna Service Release 1 (4.4.1) 和 Eclipse Memory Analyzer 版本 1.4.0

以编程方式与 Java 堆转储交互

环境设置

  1. 在 eclipse 中,帮助 -> 安装新软件 -> 安装 Eclipse 插件开发环境
  2. 在 Eclipse 中,窗口 -> 首选项 -> 插件开发 -> 目标平台 -> 添加
  3. 无 -> 位置 -> 添加 -> 安装
  4. 姓名 = MAT
  5. 位置 =/path/to/MAT/installation/mat

项目设置

  1. 文件 -> 新建 -> 其他 -> 插件项目

    名称:MAT 扩展

  2. 下一步

    • 禁用激活器
    • 禁用对用户界面的贡献
    • 停用 API 分析
  3. 下一步
    • 禁用模板
  4. 完成

代码生成

打开plugin.xml

  1. 依赖项 -> 添加
    • 选择 org.eclipse.mat.api
  2. 扩展 -> 添加
    • 选择 org.eclipse.mat.report.query
  3. 右键单击report.query -> 新建
    • 名称:MyQuery
    • 点击“impl”生成类

实现 IQuery
@CommandName("MyQuery") //for the command line interface
@Name("My Query") //display name for the GUI
@Category("Custom Queries") //list this Query will be put under in the GUI
@Help("This is my first query.") //help displayed
public class MyQuery implements IQuery
{
public MyQuery{}

@Argument //snapshot will be populated before the call to execute happens
public ISnapshot snapshot;

/*
* execute : only method overridden from IQuery
* Prints out "My first query." to the output file.
*/
@Override
public IResult execute(IProgressListener arg0) throws Exception
{
CharArrayWriter outWriter = new CharArrayWriter(100);
PrintWriter out = new PrintWriter(outWriter);
SnapshotInfo snapshotInfo = snapshot.getSnapshotInfo();
out.println("Used Heap Size: " + snapshotInfo.getUsedHeapSize());
out.println("My first query.")
return new TextResult(outWriter.toString(), false);
}
}

ctrl+shift+o 将生成正确的“导入”语句。通过访问已打开的 hprof 文件顶部工具栏的“打开查询浏览器”,可以在 MAT GUI 中访问自定义查询。

ISnapshot 界面

可用于从堆转储中提取数据的最重要的接口(interface)是 ISnapshot。 ISnapshot 代表堆转储并提供各种方法来从中读取对象和类、获取对象的大小等...

要获取 ISnapshot 的实例,可以使用 SnapshotFactory 类的静态方法。然而,只有当 API 用于实现独立于内存分析器的工具时才需要这样做。如果您正在编写 MAT 扩展,那么您的编码将通过注入(inject)或作为方法参数获得与已打开的堆转储相对应的实例。

Reference

内置命令行实用程序

如果您希望有一个程序生成通常的报告,有一个名为 ParseHeapDump 的命令行实用程序可用于任何 download of Eclipse's MAT tool 。您将能够获得 MAT 存储的所有信息的有用 html 转储。

> ParseHeapDump <heap dump> org.eclipse.mat.api:suspects org.eclipse.mat.api:top_components org.eclipse.mat.api:overview #will dump out all general reports available through MAT

希望这些信息足以帮助您入门。

关于java - 以编程方式分析java堆转储文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11127447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com