Monday, June 30, 2014

Check for Memory leaks and management in Android


There are two ways to manage memory first is Automatic memory management and second is Manual memory management.

Lets first start with Garbage collection which is a form of automatic memory management. The garbage collector or just collector attempts to reclaim garbage or memory occupied by the objects that are no longer in use by the program. Now lets see how GC works.

Every object tree must have one or more root objects. As long as application can reach those roots, the whole tree is reachable. But when are those root objects considered reachable? Special objects called Garbage-collection roots are always reachable and so is any object that has a garbage collection root at its own root.



There are mainly three kinds of GC roots :
  • Main Thread
  • Local variables in main thread
  • Static variables


To determine which object is no longer in use, the DVM intermittently runs what is very aptly called mark-and-sweep algorithmIt's a straight forward two way process:
  1. The algorithm traverses all object references, starting with GC roots, and marks every object found as alive.
  2. All of the heap memory that is not occupied by marked objects is reclaimed. 
Garbage collection is intended to remove the cause for classic memory leaks :  unreachable-but-not-deleted objects in memory. However, this only works for memory leaks in the original sense. Its possible to have unused objects that are still reachable by the application because developer simply forgot to dereference them. Such objects cannot be garbage-collected.

So, garbage collection is done intermittently by VM which can be checked in logs GC_XXXXX, which shows how much memory is been cleared. 
Note: Increasing heap for app is dangerous as then GC will take more time to traverse through the whole object tree and hence will effect performance.

Analyzing memory leaks

  • Memory leaks can be easily analyzed first by looking at log files GC_XXX/XXX... The first number after free shows how much size of objects are being used. Try doing something and observe if it varies and is huge for long time, then there is a memory leak.
  • MAT(Memory Analyzer tool) : Download MAT for eclipse, click on required process under DDMS and click Dump HPROF file, it will be downloaded, but to open it in MAT it needs to be converted. Go to sdk>tools and type this command ./hprof-conv test.hprof mat.hprof for ubuntu and open this converted file in MAT by simply clicking on open heap dump.
          There are two types of objects in heap
    • Shallow heap : just the node in tree which has its own size.
    • Retained heap : when removed from heap, it will actually remove other connected nodes.
  • How to use MAT
    • Open Dominator_tree at the bottom of Pie chart, it will show the objects taking max retained heap.
      • Right click on that object>Path to GC roots>Exclude weak references.
      • It will traverse to exact class.
    • Open Histogram at the bottom, this shows number of instances per class.
      • Right click on that object>Show object by class>by incoming references.
      • Then to GC roots and it will traverse to exact class.
      • Also here we have a small search bar, where we can see instances of our class, just type MainActivity and see. If it is more then one, then there is a leak.






0 comments:

Post a Comment