zoukankan      html  css  js  c++  java
  • [Android]生成heap dump文件(.hprof)


    Android生成heap dump文件(.hprof)

    一个heap dump就是一个程序heap的快照,能够获知程序的哪些部分正在使用大部分的内存。

    它保存为一种叫做HPROF的二进制格式。对于Android运行android.os.Debug.dumpHprofData(hprofPath)方法后所生成的文件,须要把.hprof文件从Dalvik格式转换成J2SE HPROF格式。使用AndroidSDK提供的hprof-conv工具可运行该转换操作。

    hprof-conv dump.hprof converted-dump.hprof

    本文属sodino原创,发表于博客:http://blog.csdn.net/sodino,转载请注明出处。

    相关代码能够从QQ群Code2Share(363267446)中的群文件里下载。

    Android代码生成dump文件例如以下:

        public static boolean createDumpFile(Contextcontext) {
            StringLOG_PATH = "/dump.gc/";
            boolean bool = false;
            SimpleDateFormatsdf = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ssss");
            StringcreateTime = sdf.format(new Date(System.currentTimeMillis()));
            Stringstate = android.os.Environment.getExternalStorageState();
            // 推断SdCard是否存在而且是可用的
            if(android.os.Environment.MEDIA_MOUNTED.equals(state)){
                Filefile = new File(Environment.getExternalStorageDirectory().getPath() +LOG_PATH);
                if(!file.exists()) {
                    file.mkdirs();
                }
                StringhprofPath = file.getAbsolutePath();
                if(!hprofPath.endsWith("/")) {
                    hprofPath+= "/";
                }              
               
                hprofPath+= createTime + ".hprof";
                try {
                    android.os.Debug.dumpHprofData(hprofPath);
                    bool= true;
                    Log.d("ANDROID_LAB", "create dumpfile done!");
                }catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                bool= false;
                Log.d("ANDROID_LAB", "nosdcard!");
            }
           
            return bool;
        }

    不要忘记了在AndroidManifest.xml中声明SDCard写权限:

    <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" />



  • 相关阅读:
    JS 函数—函数内部:arguments、this、,caller、new.target
    JS—函数概述
    Number API
    Object-API—02
    03构建之法阅读笔记之一
    4月第二次每周总结(4月15日)
    电梯演讲
    个人作业3—折线图
    个人作业3—china.jsp设置页面
    每周总结(4月8日)
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/6758078.html
Copyright © 2011-2022 走看看