Thursday, June 11, 2009

android memory usage with hprof

Android provides some mechanisms to generate hprof data:
  1. Dalvik VM will dump hprof file within /data/misc folder if SIGUSR1 and/or SIGQUIT signal is received. Manually send SIGUSR1 to process: kill -10 pid.
  2. The android provides monkey tool (random testing tool), with --hprof option will generate hprof file within /data/misc folder.
  3. In cupcake release, a new API has been introduced to generate a dump programmatically, the static method dumpHprofData(String fileName) of the Debug class. Example: Debug.dumpHprofData("xxx.hprof").
Some tips:
  1. The folder /data/misc shall have 777 permissions.
  2. The hprof file format is heap-dump-tm-pid.hprof-head and heap-dump-tm-pid.hprof if android 1.1 release is used.
  3. The hprof file format is heap-dump-tm-pid.hprof if android 1.5 release is used.
  4. If two files are generated, you need to join the two files into one: type heap-dump-tm-pid.hprof-head > dump.hprof; type heap-dump-tm-pid.hprof >> dump.hprof.
  5. Convert the hprof to standard hprof format: hprofconv dump.hprof out_dump.hprof
  6. Use JProfiler, Eclipse MAT, or other tools to open out_dump.hprof. I tried JProfiler and got the same problems as indicated in http://osdir.com/ml/android-porting/2009-04/msg00597.html.
Some resources:
  1. Eclipse MAT: http://www.eclipse.org/mat/
  2. MAT standalone: MemoryAnalyzer-Incubation-0.7.0.20081210-win32.win32.x86.zip
  3. Hprofconv: http://bigflake.com/HprofConv.c.txt

Monday, June 8, 2009

Android Java Space OOM

The default Android java space heap maximum limitation is 16M. If you are running multimedia applications on android, such as jpg file decoding, video decoding, you may run OOM (Out Of Memory). If memory tuning can not help you on the OOM, you can go to the file: frameworks/base/core/jni/AndroidRuntime.cpp, line 607, change opt.optionString = "-Xmx16m" to opt.optionString = "-Xmx20m" for 20M heap size or even bigger.
The limitation is the upward limitation, it will not actually allocate 20M memory every time in every java application.
Take this solution as the last try, focus on your coding quality first.

Tools to monitor your android system memory and performance

Tools to monitor your android/linux system memory and performance:
  • ps -x
  • top
  • cat /proc/meminfo
  • dumpsys meminfo proc-id
  • procrank
  • DDMS (Android SDK tool)
  • time command-line
  • vmstat

Android java space profiling tool - traceview

Android provides a good java space profiling tool: traceview. Traceview can be found in the same folder as adb in Android SDK package. Usage is quite simple and straightforward:
  1. import android.os.Debug;
  2. Debug.startMethodTracing("tag-name");
  3. Debug.stopMethodTracing();
  4. Run the Java application
  5. Fetch out the profiling output file on SD card (SD card is needed) root folder: tag-name.trace
  6. traceview tag-name.trace to open the traceview window
Insert items 1, 2, 3 into your java source code where you want to do profiling.
For more information about traceview window, refer to http://developer.android.com/guide/developing/tools/traceview.html