The entire Android developer must be experiencing a memory overflow problem with this headache once this problem. It is difficult to directly determine where our application is out of the question, in order to find a solution to the problem that must be cumulative released through some memory analysis tools for high-speed positioning and a powerful experience, now in detail there capabilities.
This feature is based on the principle of mobile phone development, low memory consumption. And my recent memory accumulation (occasional overflow) problem, summarize this problem-solving experience.
The source of the problem: when the app doesn't function as much, it doesn't notice the problem. Later, the more powerful the stronger the more. More and more pictures, with ADT's own allocation tracker to see the memory allocation, there are obviously a lot of useless data object, but not released. Start thinking is universalimageloader problem, thought this open source project on the picture loading problem, later the picture completely removed and then see memory allocation when there is useless data object, spent two days after the discovery is, Are some of their own local bitmap are not recycled and have been cached in memory. Another reason is that the previous article mentioned, in order to achieve the exit function. The use of a global ArrayList to store all newly started activity, resulting in large objects such as activity cannot be released, there are two problems with memory accumulation.
After locating the problem, replace the previous one with the previous one, so that it is half the problem and how does the local image deal with it? I read some articles on the Internet. See a lot of great gods are talking about soft reference this stuff. So we studied how to use the soft reference.
It's really a good thing to find this soft quote. It is indeed possible to optimize the memory consumption of the entire application.
Start from JDK1.2. Java divides the object into four levels to achieve the flexible control of the object's financial cycle, which is four levels strong to weak: Strong references, soft references. Weak references, virtual references. Strong references don't say much. Is that we usually direct new out of an object, do not do whatever the modification, is a strong reference. Virtual references have not been used and have not done much in-depth understanding, the use of weak references is the same basic and soft reference is the same, so focus on how the application uses soft references.
Assuming that an object has only soft references, then assuming that the memory hypothesis is sufficient, the GC will not reclaim it, assuming that the memory is not sufficient, it will first reclaim only the soft reference of the object memory, and ensure that there is no memory overflow.
This feature is based on soft references. We can use soft references to implement a fast cache of memory-sensitive areas. Therefore, in order to prevent memory overflow, we are able to use soft references, such as the context and its subclass objects, when dealing with objects that occupy large memory and have longer declaration periods. Drawable and its child objects, bitmap bitmap objects, and so on, when creating objects of these classes. Try to declare it as a soft reference.
Soft Reference Object declaration: softreference<class> instance;
The following two examples are the code that I actually use in my project. We can look at it.
This sample is used to handle large objects with long lifecycles/********************************************************** * @ file name: Activitymanager.java * @ Created: November 6, 2014 11:38:23 * @ File Description Description: Activity Management class * @ change history: November 6, 2014 Create initial version number ***************************************** /public class Activitymanager{private static Activitymanager manager = Null;private static hashmap< String, softreference<activity>> activitymap;//Static statement block, run Static{manager = new Activitymanager () when class is loaded; Activitymap = new hashmap<string, softreference<activity>> ();} Private Activitymanager () {}public static Activitymanager getinstance () {return manager;} public void put (Activity Act) {activitymap.put (act.tostring (), New Softreference<activity> (ACT)); public void Remove (Activity Act) {activitymap.remove (act.tostring ());} public void finishallactivity () {set<string> Set = Activitymap.keyset ();iterator<string> iter = Set.iterator (); while (Iter.hasnext ()) {String actname = Iter.next (); Activity currentact = ActivitymaP.get (Actname). get (); if (currentact! = null) {currentact.finish (); currentact = null;}} Activitymap.clear (); activitymap = null;}}
This sample is used to handle memory-sensitive objects such as bitmaps Demo sample public class Bitmapmanager{private static Bitmapmanager Bitmapmanager = Null;private static hashmap<string, softreference<bitmap>> imagecache = Null;static{bitmapmanager = new BitmapManager (); Imagecache = new hashmap<string, softreference<bitmap>> ();} Private Bitmapmanager () {}public static Bitmapmanager getinstance () {return bitmapmanager;} public static void Savebitmaptocache (String path) {Bitmap Bitmap = bitmapfactory.decodefile (path);// Add the object soft Reference object to the map so that it caches imagecache.put (path, new softreference<bitmap> (Bitmap));//Manually place the bitmap object nullbitmap = null after use;} public static Bitmap Querybitmapbypath (String path) {//Remove soft soft reference softreference<bitmap> Softbitmap = Imagecache.get ( path);//Use must infer whether the soft reference is recycled, returned by the empty if (Softbitmap = = null) {return null;} Bitmap Bitmap = Softbitmap.get (); return Bitmap;}}
To summarize: When we develop applications, it is best to start by recognizing that to take account of possible accounts issues, these details are done in advance to deal with the good work that will eliminate this type of problem from the root, and when the problem occurs in many other energy treatments will be spent again.
Android Memory optimization development-using soft references