All Android developers must have experienced memory overflow this headache problem, once this problem, it is difficult to directly determine that our application is there is a problem, to locate the cause of the problem, it must be through some memory analysis tools and strong experience accumulated in order to quickly locate the problem specifically there.
Based on this feature of mobile development, I summarize my experience in solving this problem with the principle of minimizing memory consumption and the recent memory accumulation (occasional overflow) problem I have encountered.
The source of the problem: the beginning of the app function is not so many times, is not aware of the problem, and later the more powerful, more and more pictures, with the Allocation tracker ADT to see the memory allocation, obviously there are many useless data object, And did not release, began to think is universalimageloader problem, thought this open source project on the picture loading problem, and then the picture completely removed and then see the memory allocation when there is useless data object, spent two days after the discovery is, Are some of their own local bitmap is not recycled, has been cached in memory, another reason is mentioned in the previous article, in order to achieve the exit function, the use of a global ArrayList to store all the newly initiated activity, resulting in activity such a large object can not be released, There are two problems with memory not accumulating.
After locating the problem, replace the previous one with the previous one, so that it solves half the problem and how does the local image handle it? I looked at some articles on the internet and saw that many great gods talked about the soft reference, so we studied how to use the soft reference. It's really a good thing to find this soft quote. It is true that the memory consumption of the entire application can be optimized.
Starting with JDK1.2, Java divides objects into four levels to achieve a program's flexible control over the object's generative cycle, and these four levels from strong to weak are: strong references, soft references, weak references, virtual references. Strong reference is not much to say, is our usual direct new out of an object, do not do any modification, is a strong reference. Virtual references have not been used and have not been thoroughly understood, the use of weak references is basically the same as soft references, so it is important to look at how the application uses soft references.
If an object has only soft references, then if the memory is sufficient, the GC will not reclaim it, and if the memory is low, it will take precedence to reclaim only the soft-referenced object memory, and no memory overflow is guaranteed. Based on this feature of soft references, we can use soft references to cache memory-sensitive areas, so in order to prevent memory overflow, we can use soft references as much as possible when dealing with objects that occupy large memory and have longer declaration periods, such as: Context and its subclass objects. Drawable and its child objects, bitmap bitmap objects, and so on, when creating objects of these classes, try to declare them as soft references.
Soft Reference Object declaration: softreference instance;
The following two examples are the actual code I use in the project, which you can look at.
Code Snippet One:
1 //This example is used to handle large objects with a longer life cycle2 3 /**********************************************************4 * @ file name: Activitymanager.java5 * @ Created: November 6, 2014 morning 11:38:236 * @ File Description: Activity Management class7 * Modified History: November 6, 2014 create an initial version8 **********************************************************/9 Public classActivitymanagerTen { One Private StaticActivitymanager Manager =NULL; A Private StaticHashMap Activitymap; - - //Static statement blocks, executed together when the class is loaded the Static - { -Manager =NewActivitymanager (); -Activitymap =NewHashMap (); + } - + PrivateActivitymanager () A { at - } - - Public StaticActivitymanager getinstance () - { - returnManager; in } - to Public voidput (Activity Act) + { -Activitymap.put (Act.tostring (),Newsoftreference (Act)); the } * $ Public voidRemove (Activity Act)Panax Notoginseng { - Activitymap.remove (act.tostring ()); the } + A Public voidfinishallactivity () the { +Set set =Activitymap.keyset (); -Iterator iter =set.iterator (); $ while(Iter.hasnext ()) $ { -String Actname =Iter.next (); -Activity currentact =Activitymap.get (actname). get (); the if(Currentact! =NULL) - {Wuyi currentact.finish (); theCurrentact =NULL; - } Wu } - activitymap.clear (); AboutActivitymap =NULL; $ } -}
Code fragment Two:
1 //This example is used to handle memory-sensitive objects such as bitmaps .2 Public classBitmapmanager3 {4 5 Private StaticBitmapmanager Bitmapmanager =NULL;6 Private StaticHashmap> Imagecache =NULL;7 8 Static9 {TenBitmapmanager =NewBitmapmanager (); OneImagecache =NewHashmap>(); A } - - PrivateBitmapmanager () the { - - } - + Public StaticBitmapmanager getinstance () - { + returnBitmapmanager; A } at - Public Static voidSavebitmaptocache (String path) - { -Bitmap Bitmap =bitmapfactory.decodefile (path); - //Add the object soft Reference object to the map to cache it -Imagecache.put (Path,Newsoftreference (bitmap)); in //manually place the bitmap object null after use -Bitmap =NULL; to } + - Public StaticBitmap Querybitmapbypath (String path) the { * //remove soft and soft references $SoftReference Softbitmap =imagecache.get (path);Panax Notoginseng //when used, it must be judged whether the soft reference is recycled, returned empty - if(Softbitmap = =NULL) the { + return NULL; A } theBitmap Bitmap =softbitmap.get (); + - returnbitmap; $ } $ -}
Summary: When we develop the application, it is best to start with the implementation of the consideration of possible problems, in advance to do these details, the root of the problem will be eliminated, and when the problem occurs, it will take more effort to deal with.
Android Development Memory Optimization Soft Reference