Android OOM (out of memory) leak BasicsReprint to: http://blog.csdn.net/emilychai2010/article/details/12710135
1. Memory leaks
2. Garbage Collection in Android
3. Heap
4. Commissioning
5. Common memory leaks
1. What is memory leak (leak)?
A "memory leak" in your code was when you keep a reference to an object so is no longer needed. Many small white, similar to me, will ask, Java does not have its own garbage collection mechanism, why is there a memory leak?
Garbage collection in 2.Android?
Garbage collection is a feature in Java, and as we know, garbage collection will reclaim all objects that cannot be touched by GC root object. The analysis starting point of Oom is garbage Collection Roots. Any object that can be reached through GC root will remain in memory, and objects that cannot be reached through GC root will be recycled. For example, the red is GC root, the blue circle represents the object that can be reached by GC root, and the black one will be recycled by GC. The blue is left in memory, which is the point where oom occurs.
Figure 1
GC root object includes:
(1) objects in the current call stack, including local variables and method parameters
(2) JNI native references
(3) Classes loaded by all boot programs (bootstrap Loader)
(4) The process itself
Before gingerbread, garbage collection used the "Stop the World" strategy, which means that when garbage collection, all programs must be stopped, the entire heap recovery, starting from gingerbread, garbage collection is using a synchronous recovery mechanism, Partially recycled, the pause time is usually less than 5ms.
For example, bitmap before Honeycomb program through recycle (), finalizer for recycling, garbage collection takes the whole heap of recycling, "Stop the World". After honeycomb, the system recycles the bitmap in a synchronous recovery.
3.Heap
Dalvik.vm.heapsize determines the size of the heap. In Gingerbread, Android introduced a new android:largeheap, but with this option to be cautious, is not your program really need such a large heap? Because the big heap means longer garbage collection.
Several important concepts
Shallow heap: the memory consumed by an object. Retained set of x: When x garbage collection is in progress, a series of objects will be deleted by the GC collection. Retained heap of x:x keeps active memory. The shallow heap of an object is its size in the heap, and its retained heap refers to the amount of memory that can be freed in the heap when the object is garbage collected. For example, 2, the yellow origin of the shallow heap isretained , the heap is 400.
Figure 2
dominators: If object A dominates object B, it means that every road leading to B goes through a. The GC reclaims all objects that are not referenced. For example, a is deleted in memory, there is no way to go to B, this time B will be collected by GC.
Dominator Tree: The relationship of organizing memory objects with the Dominators idea is Dominator tree. Each node of the tree stores memory that can be freed. You can see from this tree that the largest piece of recycled memory and the dependencies that remain active in each object.
Reference Object :
|
Strong reference |
That are never recycled by GC.
Object O=new object ();
Object o1=o; |
|
Soft Reference |
It is not normally recycled by GC and is only recycled with significant memory operational requirements.
Memory-sensitive Caches |
|
Weak Reference |
Can be recycled by GC. Weak Reference is a reference that is not strong enough to force an object to remain in memory. If an object is simply weak referenced, it means that it may be recycled by GC at any time. |
4. Memory Leak debugging
The difference between oom and other bugs is that log may not be useful at all. In the time of sending oom, the whole system is in memory saturation, a program hangs off is very normal thing, so log is not much reference, the most important is the Hprof file. It reflects the state of memory usage at this moment. I used Eclipse's mat to analyze Hprof.
First, the hprof in the Android phone is going to be converted into Eclipse's recognizable format. The Android SDK comes with this conversion tool, and in the SDK's Tools directory, enter this command to convert the outofmemory0.hprof in the phone to eclipse-recognizable new.hprof.
[Java]View Plaincopyprint?
- Hprof-conv outofmemory0.hprof/share/new.hprof
Then import this into eclipse. Mat will give a questionable analysis, list dominate Tree,histogram view and so on, you can see how many of an object, how much memory, who has been holding it and so on. is a very useful tool. The mat's help document is also very clear, so you can go and see it.
5. Common memory leaks
(1) The holding object is not released.
This is a very typical oom:activity a oncreate () in which I recently met, b b = new B (mcontext); In the construction method of B, a listener is registered, but in Activity a The OnDestroy () There is no anti-registration for this listener, causing this listener to persist. So this listener has been holding mcontext, as seen from the Hprof file, there are 7 instances of Activiy A.
In other words, the reference to Activity a does not conform to the life cycle of a. After a is destroyed, A's reference is still alive.
The final solution is to replace the activity context with the application context, which Google advocates as a way to avoid oom.
Why is that? Perhaps my understanding is not quite in place: because theoretically application only one, and activity can have multiple. Application context guarantees that only one of the references to application is available throughout the application life cycle.
(2) cursor does not close.
This is the most common operating database caused by a memory leak. In the case of small amount of data and few operation times, there is no special performance of the system. However, once the amount of data and the number of operations become more, memory leaks will occur. So the cursor must remember to close after use. This should be common sense, but it is true that there are problems in real projects that are caused by this cause.
(3) Picture problem
(4) Non-static internal class problems
(3) (4) Back to the update, experience is still shallow, indeed did not met.
Oom is still a very complex problem, it is estimated that they do not even understand the fur, summed up this also has a lot of mistakes, but in the future will be constantly updated and perfected. Experience this oom, just a feeling, everything you fear is a paper tiger. The following link is well written and left to look over and over again. PS Why people write the technology is so logical so clear that?
Http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html
Http://kohlerm.blogspot.com/2009/07/eclipse-memory-analyzer-10-useful.html
Http://android-developers.blogspot.com/2011/03/memory-analysis-for-android.html
Android OOM (out of memory) leak basics