Analyzing the memory usage of your Android Application

Source: Internet
Author: User

The new Android 1.5 early look SDK is out since a few weeks. the "android 1.5 highlights" page does not mention one highlight, which IMHO will become very important for all developers on the Android platform because it allows you to find memory leaks easily and analyze the memory usage of your android applications.

I'm talking aboutHprof-Conv ToolThat allows you to convert an android/Dalvik heap dump into. hprof heap dump. the tool does not preserve all the information, because Dalvik has some unique features such as cross-process data sharing, that are not available in a "standard" JVM. still. hprof file is a good starting point and can be read with the eclipse memory analyzer, which will interpret it like a normal file from a sun 32 bit JVM. in the future it shoshould also be not that difficult to read the Dalvik heap dump format directly and provide more information for memory analysis, such as which objects are shared and also maybe how much memory is used by native objects (haven't checked that ).

How to get a heap dump

First we must of course get a heap dump. I do not yet own an Android phone, also I plan to get on this year. if you want to donate one, please let me know. donating lot's of money wocould also help
I therefore created the heap dump using the emulator.
I created a new AVD for the 1.5 preview:

Android create AVD-n test-T 2
Emulator-AVD Test

Then you can logon to the emulated phone and make sure that the/data/MISC directory is writable:

ADB Shell
Chmod 777/data/Misc
Exit

Heap dumps are actually created by sending the process a SIGUSR1 (-10) signal.

ADB shell PS

Check for the PID of your application

ADB Shell kill-10 PID

You can check

ADB shell logcat

Whether it works. You shoshould see something like:

I/dalvikvm (): threadid = 7: reacting to signal 10 I/dalvikvm (): SIGUSR1 forcing GC and hprof dump I/dalvikvm (): hprof: dumping VM heap to "/data/MISC/heap-dump-TM-PID. hprof-hptemp ". i/dalvikvm (): hprof: dumping heap strings to "/data/MISC/heap-dump-tm124026 3144-pid.hprof ". i/dalvikvm (): hprof: heap dump completed, temp file removed D/dalvikvm (): GC freed 1292 objects/92440 bytes in 11 sec D/dalvikvm (): GC freed 215 objects/9400 bytes in 963 Ms

Now you can copy the heap dump from the (emulated) phone to the desktop machine:

ADB pull/data/MISC/heap-dump-tm-pid.hprof address. hprof

Now the file you will get does not conform to the "standard" Sun. hprof format but is written in Dalvik's own format and you need to convert it:

Hprof-Conv heap-dump-tm-pid.hprof 4mat. hprof

Now you can use the eclipse memory analyzer to analyze the memory usage of your application.

Typical optimization opportunities

I described some typical issues already here in this blog for example a problem with finalizers in netbeans,
Or finding "string duplicates" in eclipse (my favorite memory analysis trick ).

You might also take the time and check all my memory related posts.

Now I wowould like to present you a new typical memory usage issue that I just found when playing around with the new Android cupcake 1.5 prerelease.

Youshouldhavedonelazyinitialization


I did a heap dump of the androidGmailApplication and loaded it into the eclipse memory analyzer.
In the histogram view I filtered the android classes:

The high number of rect (rectangle) Objects looked suspicious to me. Also they retain not that much memory, I thought it wocould be interesting why such a high number of rect instances was alive.
When checking the rect objects I immediately saw that most of them seemed to be empty e.g. Bottom = 0 and left = 0 and Right = 0 and Top = 0.

It seemed to me that this was the time to again use the most advanced feature in mat. i'm talking about the oql view, which allows you to execute SQL like queries against the objects in your heap dump.
I very rarely used this feature in the past because the standard queries are good enough almost all times and because writing your own command in Java is not that difficult.
Still Here It wocould help me to find how useful of the rect instances where empty.

I entered

Select * from Android. Graphics. rect where bottom = 0 and left = 0 and Right = 0 and Top = 0

Which showed me that out of 1320 rect instances 941 where "empty", which means that over 70% of the instances where "useless ".
I used "immediate dominators" on the first 20 of those 941 instances, which showed me that those empty rect instances were retained by several UI related
Classes which are subclassesAndroid. Graphics. drawable. drawable.

I checked the source and quickly found that drawableAlwaysInstantiates a rect using the default constructor:

public abstract class Drawable {    private int[] mStateSet = StateSet.WILD_CARD;    private int mLevel = 0;    private int mChangingConfigurations = 0;    private Rect mBounds = new Rect();

Also this issue might not increase the memory usage of Gmail that much it's still a fundamental/systematic problem, which IMHO shoshould be fixed.
Even if we wowould tolerate the memory overhead, we are still left with the problem that the number of objects the garbage collector has to trace during full GC is higher than needed, which at least potentially can lead to sluggish UI.
It's also a kind of fundamental problem becauseAll subclasses of drawable inherit the problem.

So instead of always using the default constructor it wowouldMost likelyBetter to use lazy initalization and only initialize the field when it's first needed.
A copy on write strategy cocould also be used by sharing an "empty" rect instance in the beginning, which wocould be replaced by a new one as soon as the field is written (haven't checked the Code whether this can still be done ).

Disclaimer:
This is not a sign that android is a poor platform. this is a kind of a problem that I 've seen in the past more than once and I'm sure this kind of antipattern can be found in a lot of software out there.

Update:


The issue has been fixed a few hours (or minutes ?) After my post!
Check
Https://review.source.android.com/Gerrit#patch,sidebyside,9684,2,graphics/java/android/graphics/drawable/Drawable.java

To see how simple the change was!

Just take the eclipse memory analyzer and have a look at your application (s ).
May the Force be with you!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.