WRANGLING DALVIK: MEMORY MANAGEMENT IN ANDROID

来源:互联网 发布:接口压力测试python 编辑:程序博客网 时间:2024/04/29 14:07

转:http://www.raizlabs.com/dev/2014/03/wrangling-dalvik-memory-management-in-android-part-1-of-2/


Update: Part 2 is now up! Check it out!

There are some people who believe this myth that you don’t need to worry about managing memory while writing Android applications. It’s a self-contained Java environment, right? What’s the worst that could happen?

Well, it’s true – the Android OS, through the Dalvik runtime1 (now being superceded in some cases with ART), doesn’t have to worry about your app crashing the whole system due to poor memory management. But, alas, that doesn’t mean that your app doesn’t have to deal with managing its memory: Dalvik will be happy to kick you out of execution if you screw up your heap allocation, and your users will start leaving negative reviews about all the crashes they’re getting.

By the way, what we’re looking for throughout this exploration of potential memory problems is a crash called an OutOfMemoryError (more notoriously and affectionately known as an OOM): thrown by the application when you try to allocate something past the capacity of the heap. It’s important to note that this can happen at any time, caused by any object, so that doesn’t provide a very good reference of what’s actually the cause of the crash (probably something you’ve “leaked” into memory earlier).

Simply, you’re going to have to face it and trace it back to its source. But where do you start?

LESSON #1: YOU’RE NOT GOING TO FIND IT BY ACCIDENT

Don’t trust your own test devices to fail in all of the remarkably innovative ways your users’ devices will manage to fail.

Here are several tips:

  1. Understand potential sources of issues: if you know what not to do, you never need to know how to fix it.
  2. Plan for a lot more QA than you ever expected.
  3. Very thorough unit testing.
  4. Run analyses on builds before shipping (we’ll cover this next week, in Part 2).
  5. Utilize prayer, lucky rabbit feet, or indomitable willpower in the face of the inevitable flood of negative reviews when you ignore this issue.

The best way to effectively pass timely, event-driven data around in Android ecosystems can be debated without end. And, in fact, it often is. But, that’s not the purpose of this post.

One method is attaching an implementation of an interface definition to the object sending the data, and holding that implementation within the receiver object.

At Raizlabs, one of our go-to simple-syntax elements is an interface called EventListener:

EventListener might be used to receive data from a separate object, like an Activity class which wants to get notifications when a given Object is updated successfully:

And, in SomeObject, we might see the following, indicating that our data has been saved successfully:

Which would notify the EventListener instance that we created in the Activity’s onResumemethod that the event has successfully been completed.

Now, for those well-seasoned memory wranglers, seeing the immediate danger of this kind of pattern must be super easy. But, for the rest of us, we’re going to have to keep an eye out for problems like these. As it turns out, we just leaked an Activity. And most Android devices don’t need many Activities before you hit an OOM.

LESSON #2: STALK YOUR REFERENCES

You’re going to have to keep an eye on what you reference in the course of developing an Android app. For iOS developers reading this, you don’t have to worry about retain cycles, since Dalvik’s GarbageCollector will parse the entire map of referenced objects. But, you may accidentally hold strong references to objects you no longer need, and in that case, the GarbageCollector will pass over those unneeded objects. For example, a Context that is no longer on screen, or a bitmap that won’t be displayed again for a while).

Anytime you find yourself creating a reference (that is, any time you assign a variable to an Object), think about what you might need to do to prevent a memory leak.

If the reference is to an object that is instantiated inside your own class, you usually2 don’t have to worry about it. That is, unless you’re sharing strong references with a different object. If that’s the case, then you must manage their references more manually.

For instance, in the example above: we’ve attached a reference to our Activity instance to some object, presumably persistent, and in a manager somewhere. The point is, the Activity doesn’t know that the lifespan of SomeObject will end when the instance of the Activity ends. If that object remains in memory, it will hold that Activity in memory as well, even after the Activity has been visually destroyed (a user going back, or rotating the device, or some other lifecycle-ending event). So, what we need to make sure to do, is remove that reference inonDestroy() or similar end-of-lifecycle-method:

LESSON #3: SURVIVAL OF THE FITTEST REFERENCES

Android contains a few different possible types of references. Each level of strength indicates how the system’s GarbageCollector will interact with that Object.

The reference you’re inevitably used to using is a Strong reference. This is what you get when you do:

A strong reference means the GarbageCollector will ignore the instantiated object “Hello World” as long as the “pointer” myVariable references it. Once you set myVariable = null;, that String can be swept up out of memory at any time. (Not that it matters anymore, since you no longer have a reference to that string! You just have a reference to null).

The rest of the reference types are actually a part of the SDK, and they are all created in the same general way. We’ll use a SoftReference to demonstrate:

A Reference object will give you access to that “Hello World” string, but at any point the class could decide to reassign myStrongVariable, like so:

Now that object we have a Reference to, “Hello World”, might be GarbageCollected at any time. You can try to access it:

But you can’t be sure testString is not null. So, how do each of the non-Strong reference types stack up?3

  • SoftReference: if there are only SoftReferences to an Object, the reference will generally be held on to as long as there’s memory for it to fit.
  • WeakReference: if there are only WeakReferences to an object, it will be purged from memory at the next GarbageCollection cycle.
  • PhantomReference: the weakest and most enigmatic reference, that get() method will always return null – you can never access the Object it references (even when the Object still exists, and has other references). Realistically, you should probably never be using these.4

…With great knowledge comes great responsibility.

Just because you now know the difference between these references, doesn’t mean you should be throwing them around as a solution to memory management without a very good reason.

The use of the Reference classes is usually a sign of a dirty design pattern. The first thing most people think of when they learn about SoftReference is, “Oh, what a good idea for designing a cache system!” But, while a decent idea, that’s an official Android no-no.

LESSON #4: “THERE IS NO GARBAGECOLLECTOR, NEO”

Occasionally, I’ll see a memory-related StackOverflow answer that suggests, as a production-environment solution, the line:

And when this happens, I cry myself to sleep.

The Android GarbageCollector is not some pet that you should whistle for when you want it to come play. It’s slow, and heavy-handed, and totally and completely capable of doing a great job without your input.

When an activity lifecycle ends (e.g. pressing back, or rotating the device) – that is, onceonDestroy() has finished – it should have no references whatsoever still pointing to it. Otherwise, the GarbageCollector will politely ignore it, thinking you’re not done with the Activity object, and you’ll wind up with some massive memory leak.

OK, this should be in the most tiny font we have available, but… once in a while, and we’re talking every four or five blue moons at the most, you may use System.gc(). But, if you can’t write at least twelve reasons that’s your only solution, you should probably re-think your memory management design.

LESSON #5: DON’T HOLD ON TO REFERENCES TO ACTIVITIES. EVER.

This also applies to Fragments, Views, Resources and anything else tightly associated with a Context. These kinds of objects are sprawling metropolises of references to absolutely everything. We can get more into investigating the runtime memory map of your app a bit later, but until then, the tangled web we Context-based objects weave is insidious.

If you accidentally hold a reference to a View beyond its lifetime (i.e., you “leak” it), it has a reference to the Activity context that created it, and in turn, every other View, Fragment, Dialog, and so forth associated with that activity.

And if this happens, you’ll not be able to get it out of memory. So, you can kiss handful after handful of MBs goodbye each time this happens.5 If you’re ever having memory trouble while developing an Android application, the very first thing to check for is leaked Activities.

LESSON #6: IT CAN HAPPEN TO YOU…

I wouldn’t be writing this if it wasn’t something we recently ran into. Fortunately, we caught the problem before shipping to the Play Store, but it was entirely possible that it could have made it through.

Our problem? We had tripped over the fine line between abstraction and obfuscation.

Story time!

We had a bunch of activities that wanted to know when someone logged in; so, when the Activity was created, we would instantiate a class variable of that type EventListener, and add it as a reference to the singleton class (as in, an object that is never GarbageCollected in the lifespan of the app) that managed our user authentication. Then, of course, we would remove the reference in the Activity’s onDestroy() method like good little memory managers.

Alas, the key we had missed: we use many fragments. Occasionally, we have to reference the parent activity from a child fragment. When one of our fragments wanted to get that login notification, it simply retrieved its parent activity, and called the method:

But, that method’s implementation was designed to add the listener straight on to the singleton that managed authentication. We never bothered removing it, because that method looked like it was adding a reference to the Fragment to its parent Activity – which would allow the Fragment to be GarbageCollected at the same time as that Activity. Since this method was just a convenient proxy to add a reference to the authentication singleton, we wound up with a leaked reference to the Fragment, and, in turn, a reference to that Fragment’s context (its parent Activity) and all of its references in turn.

WAIT, THAT’S IT?

Yes, this concludes Part 1, wherein we’ve gone over some details of how memory can be leaked. If you’re wondering how to fix all the problems you never knew existed until just now, we’ll get into how to analyze your app’s memory usage in Part 2.


USEFUL LINKS:

  • Android training docs on memory management
  • Patrick Dubroy on Memory Management


  1. Dalvik is a virtual machine that represents the heart of the Android operating system. It is the system the OS uses to sandbox your app into its own virtual space, providing a unique and isolated heap, and compiling and executing the runnable .dex files (Dalvik Executables). Dalvik also happens to be the main village in Dalvíkurbyggð, Iceland.↩
  2. An important caveat here includes inner class definitions. I won’t get into the technical details of non-static instantiations of private static inner classes, but Patrick Dubroy has a marvelous explanation in his Google IO talk on memory management.↩
  3. Pun actually unintended.↩
  4. Fundamentally, you can use them to trace memory patterns in an app with a PhantomReference. You can’t get to the Object itself, but you can tell when an object is about to be cleaned up by GarbageCollection, and therefore, no longer valid. But, please don’t use this if you don’t have a very good reason to.↩
  5. Some people may not be aware of how the heap works on Android. Sorry for the oversight, but here’s a little refresher: The heap is space the Android operating system gives your app for quick access to data. This has to do with all that RAM stuff you kids talk about. You want to maximize your use of this space while you’re writing an app without going over the allotted size. If you do, you’ll get an OutOfMemoryError and your app will be guillotined. The Android OS cleans up your memory for you (GarbageCollect) when it gets close to the heap limit. It’s also important to note that different devices offer different amounts of heap space. Typically, you can expect somewhere between 16MB to 128MB of available heap space on most phones. That said, it’s important to monitor it carefully.↩

2 THOUGHTS ON “WRANGLING DALVIK: MEMORY MANAGEMENT IN ANDROID (PART 1 OF 2)”

  1. Pingback: Hunting Your Leaks: Memory Management in Android (Part 2 of 2) | Dev Blog

  2. Excellent article. I faced a similiar memory leak just now. I just had a doubt how the someObject holds a reference to the activity. Is it because in:-
    object.setSuccessListener(new EventListener()
    the new EventListener is an anonymous inner class and holds an implicit reference to the activity?


0 0