1

I am debugging a Android slideshow playing app in eclipse. I am trying to generate heap dump in order to find the origin of memory leak but no hprof file is generated in the folder"/data/data/app folder/"android device I am testing. The following is my code for generating the files. What's wrong with that? Thanks in advance!

public class HeapDumpingUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
private static final String HPROF_DUMP_BASENAME = "leak-app-";
private final String dataDir;

public HeapDumpingUncaughtExceptionHandler(String dataDir) {
    this.dataDir = dataDir;
    Date d = new Date();
    CharSequence s  = DateFormat.format("yyyy-MM-dd_HH-mm-ss", d.getTime());

    String absPath = new File(dataDir, HPROF_DUMP_BASENAME +s+".hprof").getAbsolutePath();

                     try {
                    Log.d(this.getClass().toString(),"memory leak app handler: initial heap dump created "+ absPath);
                         Debug.dumpHprofData(absPath);

                     } catch (IOException e) {
                         e.printStackTrace();
                     }
}

@Override
public void uncaughtException(Thread thread, Throwable ex) {

    Date d = new Date();
    CharSequence s  = DateFormat.format("yyyy-MM-dd_HH-mm-ss", d.getTime());

    String absPath = new File(dataDir, HPROF_DUMP_BASENAME+s+".hprof").getAbsolutePath();

    Log.d(this.getClass().toString(),"memory leak app handler: exception caught : "+ex.getClass()+ " heap dump path "+absPath);
        try {
            Debug.dumpHprofData(absPath);
        } catch (IOException e) {
            e.printStackTrace();
        }

    ex.printStackTrace();
}
}
Anndexi9
  • 141
  • 2
  • 15
  • 1
    You should try https://github.com/square/leakcanary to get help on identifying memory leaks. When one occurs the library creates an hprof file but most of the time you can find the problem only reading the exact line when something leaked. BTW you should add the UncaughtHandler in a custom extended Application class like done here http://stackoverflow.com/a/8943671/2910520 – MatPag Feb 08 '17 at 01:14
  • Thank you but I can't get how to install it in eclipse even I googled it. Almost no tutorial out there for eclipse. – Anndexi9 Feb 08 '17 at 01:48
  • 1
    I suggest you to switch to Android Studio, a lot of the new tutorials are based on AS... it's worth for sure. – MatPag Feb 08 '17 at 15:15
  • I can't coz it is the project for work, not personal... – Anndexi9 Feb 09 '17 at 00:04
  • so is there any problem in my code for generating hprof? – Anndexi9 Feb 09 '17 at 00:06

1 Answers1

0

Finally find out I dun reli need this code to generate hprof as DDMS already has this function. Sorry for disturbing you guys and thank you.

Anndexi9
  • 141
  • 2
  • 15