Saturday, April 13, 2013

Memory Management in Android

What is Memory?
What is Management ?
What is Memory Management in Android ?
Why we take care of memory management related problems in Android ?
When we take care of memory management in Android ?
What is heap size and how to manage heap size ?
What is memory leak ?
What is MAT (Eclipse Memory Analyzer tool)
What is GC (Garbage collection) and use of gc ?



Android Memory

Android is a Linux based OS with 2.6.x kernel, stripped down to handle most tasks pretty well. It uses native open source C libraries that have powered Linux machines for years. All the basic OS operations like I/O, memory management, and so on, are handled by the native stripped-down Linux kernel



Memory Analyzer (MAT)


Android process and memory management is a little unusual. Like Java and .NET, Android uses its own run time and virtual machine to manage application memory. Unlike either of these frameworks, the Android run time also manages the process lifetimes. Android ensures application responsiveness by stopping and killing processes as necessary to free resources for higher-priority applications

Each Android application runs in a separate process within its own Dalvik instance, relinquishing all responsibility for memory and process management to the Android run time, which stops and kills processes as necessary to manage resources.

The Dalvik Virtual Machine -

One of the key elements of Android is the Dalvik virtual machine. Rather than use a traditional Java virtual machine (VM) such as Java ME (Java Mobile Edition), Android uses its own custom VM designed to ensure that multiple instances run efficiently on a single device.

The Dalvik VM uses the device’s underlying Linux kernel to handle low-level functionality including security, threading, and process and memory management.

All Android hardware and system service access is managed using Dalvik as a middle tier. By using a VM to host application execution, developers have an abstraction layer that ensures they never have to worry about a particular hardware implementation.

The Dalvik VM executes Dalvik executable files, a format optimized to ensure minimal memory foot- print. The .dex executables are created by transforming Java language compiled classes using the tools supplied within the SDK.





Garbage Collection (GC) is the process of reclaiming old memory in your application. Or in other words, taking objects that are no longer in use and freeing the memory used by them so the memory can be used for new objects.


  • The Heap is where all applications are stored within Android.
  • Each Android app has a limit that is specific to each device. 
  • To find that limit, use the method ActivityMonitor,getMemoryClass.


Sometimes that Heap size isn't enough. Luckily, there is a way to increase the Heap size for your app. To increase the size, add the tag bigHeap to your Application tag in the manifest file.

By having a large Heap, GC can take considerably longer because GC has to go through a larger Heap to check for objects that can be freed. 

So just using this tag to get away from memory leaks in your app is a pretty bad idea and will only make your app performance decrease

Memory Leak -  a memory leak means you still have a reference to an unused object, which means GC can't remove an object. One common memory leak in Android involves keeping a reference to an Activity(or a context) for far too long. This is especially common when handling Activity rotation changes.

If you happen to pass Context objects around frequently in your application, it's much more likely that a memory leak has crept into your app.


The Eclipse Memory Analyzer is a fast and feature-rich Java heap analyzer that helps you find memory leaks and reduce memory consumption.
Use the Memory Analyzer to analyze productive heap dumps with hundreds of millions of objects, quickly calculate the retained sizes of objects, see who is preventing the Garbage Collector from collecting objects, run a report to automatically extract leak suspects.


Heap size is defined as –
• Heap size limits
– G1: 16MB
– Droid: 24MB
– Nexus One: 32MB
– Xoom: 48MB
• ActivityManager.getMemoryClass()

LargeHeap –

We can explicitly allocate application with large heap, to change in Manifest file –
<application
 android:name="com.example.foobar"
 android:largeHeap="true"
 ...
 </application>
But it will degrade performance of system.
Use can also use ActivityManager.getLargeMemoryClass() at activity level.

Garbage collection –
• Bigger heaps = longer pauses?
• Pre-Gingerbread GC:
– Stop-the-world
– Full heap collection
– Pause times often > 100ms
• Gingerbread and beyond:
– Concurrent (mostly)
– Partial collections
– Pause times usually < 5ms
















keyword - memory management, heap size, gc,