Respect the original, reproduced please indicate the source: Lee http://blog.csdn.net/hnulwt/article/details/44900761
Why Oom is producing
Oom can occur because 1, loading a large picture causes memory to overflow 2, a lot of memory leaks
What is the nature of oom production?
Dalvik VM Main management of the memory Java heap, due to the limitations of mobile devices, generally an application uses memory can not exceed the default value of 32M (different devices slightly differ, through adb shell Getprop | grep dalvik.vm.heapgrowthlimit Command view), which means that when the heap memory requested on the DVM is greater than the default threshold, our application throws OutOfMemoryError.
How to solve and avoid OOM1, solve large picture cause memory overflow loading multi-graphs: (1) using soft references, weak references, when the heap memory is low, you can automatically release these cached bitmap objects.
Here is an article on the description of the bitmap allocated on the native heap or the Dalvik heap.
Notes on soft references:
Soft references (softreference) are used to design object-cache. He clears all soft references before the JVM reports out of memory, so that the GC has the potential to collect soft-and-weak objects, possibly addressing out-of-memory problems and avoiding memory overruns. When it is collected depends on the GC algorithm and the size of the available memory when the GC is running.
Notes on weak applications: it's easier to read an example:
String test =newString("aaa"); WeakReference<Stringnew WeakReference<String>(test); null; System.out.println("before: "+ testWeak.get()); System.gc(); System.out.println("after: "
Results:
Before:aaa
After:null
If you want to get information about an object at any time but do not want to affect the garbage collection of this object, you should use a weak application (Weak Reference) to remember this object.
(2) Used diagrams are no longer used and can be called bitmap.recycle () to expedite recycling.
if (null != bitmap && !bitmap.isRecycled()) { bitmap.recycle(); }
(3) Consider using file caching. The entire large map needs to be loaded:
Get bitmap before you take advantage of Bitmapfactory.options's insamplesize worth to compress the picture.
Key code:
The first resolution sets Injustdecodebounds to true to get the picture size final bitmapfactory. Optionsoptions = new Bitmapfactory. Options(); Options. Injustdecodebounds= True; Bitmapfactory. Decoderesource(Res, resId, options); Call the method defined above to calculate the Insamplesize value, the Calculateinsamplesize method writes itself, here no longer repeat the options. Insamplesize= Calculateinsamplesize (Options. Outwidth, Options. Outheight, Reqwidth, Reqheight);Options. Injustdecodebounds= False; Bitmap bmp = Bitmapfactory. Decoderesource(Res, resId, options);
Load only some pictures
You can consider the introduction of the Bitmapregiondecoder class after API 10, the specific use of the method has not been studied, the source annotation (Bitmapregiondecoder is particularly useful if an original image is large and you are only need parts of the image).
2. Resolve Memory leaks
To solve this problem, we need to have some more in-depth understanding of the components of Android system, such as:
When you understand the life cycle of an activity, you should avoid references to the life cycle. An application may have multiple activity components, which should be considered using the application class. (This problem is mainly for the control of static objects in activity.)
Try not to recycle a block of memory due to a variety of complex references that prevent the GC from being able to recover in time or even never.
These are some of my solutions to the oom problem, if you have other good ways to ask for a common promotion.
Why Android Oom causes and how to solve