memory management

Download Memory management

If you can't read please download the document

Upload: miteshsharma

Post on 29-May-2015

310 views

Category:

Mobile


2 download

DESCRIPTION

Explaining memory management in android

TRANSCRIPT

  • 1. Memory Related issues, why?

2. Memory Related issues, why? Each application runs in a linux process, havinga Virtual machine (Dalvik VM) running inside it. Hard Limit of memeory is present based ondevice heap size which can be 16MB, 32MB,64MB. If application demand more memory then limit,it gives OutOfMemory error. 3. Memory Related issues, why? Some operation which demands a lot ofmemory even when we don't have. Leaking memory i.e. some object which is notunder use but still not available for GC. Dealing with large bitmaps and loading themtogether. 4. Heap Sizes on different devices Heap Size Limit G1 16MB Droid - 24MB Nexus One 32MB Galaxy S3 64MB 5. Memory Related issues: How tofind? 6. How to find memory Leak Can be done using logcat logs: [Reason] [Amount Freed], [Heap Statistics],[External Memory Statistics], [Pause Time] GC_EXPLICIT freed 4178K, 18% free26631K/32304K, paused 3ms+9ms, total166ms The GC_EXPLICIT happens when we call theSystem.gc() 7. Reasons of memory leakGC_FOR_MALLOC/GC_FOR_ALLOC(new name) means that the GC was triggered because therewasn't enough memory left on the heap to perform an allocation. Might be triggered when newobjects are being created.GC_EXPLICIT means that the garbage collector has been explicitly asked to collect, instead ofbeing triggered by high water marks in the heap. Happens all over the place, but most likely when athread is being killed or when a binder communication is taken down.GC_CONCURRENT Triggered when the heap has reached a certain amount of objects to collect.GC_EXTERNAL_ALLOC means that the the VM is trying to reduce the amount of memory used forcollectable objects, to make room for more non-collectable.GC_BEFORE_OOM means that the system is running really low on memory, and that there is afinal GC performed, in order to avoid calling the low memory killer. 8. Memory Related issues, How toDebug? 9. Debugging Check Heap size Dump Heap at different points and find itemstaking lot of heap using MAT Use Allocation Tracker to check what allobjects are allocated in a time period : try toanalyze 10. Debug using MAT Install eclipse plugging : Memory Analyzer Dump Heap using DDMS and open it in MAT. Check memory usage and places where it isbeing used. Check Histogram : List of number of instancesper class Dominator tree: Gives domanint node for eachmemory allocation. Gives other useful data for memory analysis. 11. MAT terminology Shallow Heap Retained Heap Dominator tree 12. Debug using MAT Compare two heap dumps using MAT toidentify difference at two instance. Open the first HPROF file (using File > Open Heap Dump). Open the Histogram view. In the Navigation History view, right click on histogram and selectAdd to Compare Basket. Check the difference in memory allocation at two instances 13. Useful Adb Commands 14. Adb commands Adb shell ps gives processes running adb shell dumpsys meminfo PID-> givesmemory info of process adb shell cat /proc/meminfo-> gives asummary of the overall memory usage of thesystem.