In the development of Android project, memory problem has always been a very common problem, in the actual development, how to be good at the development of the constraints & control of memory has become a hate important thing.
1. Why do I need to constrain memory at development time?
Because of the development of different types of apps, the hybrid language development is the most common one in our development now, and in different scripting languages, our memory generation is not the same.
The following small compilation takes 1) very native APP2) hybrid Web App3) Lua scripting language app
Note: Because of my computer problems, can not add pictures, concrete proof that the class picture will be in the late idle time to add
Result: In these three situations the layout produces the memory is different, also has many CE to say is because some of their framework principle is not the same result, indeed, Lua script is uses the reflection, hybrid more is uses addjavascript function, In the simplest case and in the second case, we will find that when we create a linear layout, 3) produces more memory than 1, and writes the Recycle method, we will find that when we call Ondestory () to reclaim the function, 1) is quickly recycled. and 3 is not necessarily, gu we come to the following conclusions, in the use of different frame construction, will have a great impact on our memory generation, GU, we must pay attention to the use of the framework in the initial stage, use of what mixed language development method, if not in the development of the time to constrain memory, When we develop the test phase in the constraints control the memory will find the exception of the tricky
2. Resource Files
From Java to do Android, it is found that the image format of the Android image algorithm is really super scary, just a small picture, looking at the 2,3KB, the calculation is very large, gu resources picture size, As well as the reading method (this refers to the PS in the picture to save the number of read bits and image clarity, etc.) will greatly affect memory, especially in the poor in many large companies to promote plug-in, hot update, in the framework implementation and, picture loading, the picture will be pre-compiled and loaded apk, will make the image of the KB content of the scary, how to control it,
1> observation, first of all, look at the picture, see if it can be made. 9 figure, if you can make a point nine, be sure to make a point 9 figure
2> picture compression, a lot of CE will think the number of KB of the image has little impact on memory, the size of the image is the largest memory impact of the head
In fact, in the actual development, we often find that a lot of company designers in the transduction, and will not compress the picture, when we take the 100kb image directly filled with the view, more computing we will find the memory super scary, gu oom is also very common things are not right. We're going to develop ourselves a compressed image at this time, I was asked by my colleague before. 9 diagram can also be compressed, after my experiment,. 9 figure is able to do the right amount of compression, but can not compress too hard, otherwise it will lead to read the exception, the general reading abnormal performance, that is, the. 9 figure as a normal pattern read, so need special attention to ha.
3> Resource picture Use, we must pay attention to the image reuse, if the box type is the same, it is best to write the XML shape directly instead of using multiple pictures
3. Single row "program code"
In our actual development, will still write a lot of single-row methods, such as we often use the common method of NULL, as well as the mask phone number, mask ID and other such methods, the Singleton is a global static object, when holding a copy of the Class A is, a cannot be freed, memory overflow, Gu must pay attention to the occasional.
4. Internal class "program code" "Reference http://www.cnblogs.com/deman/p/5860976.html"
The inner class (anonymous inner Class) in Java holds a strong reference to the host class, this.
So if this is the new thread, the operation of the background thread will not be recycled when the thread has not finished executing.
Context references, when TextView, and so on, will hold references to contexts. If there is a static drawable, it will cause the memory to not be freed.
5.java data generates memory
Before working at Fumin Bank time, then PM boss, from Beijing, to sister said, Java data generated memory can not be considered, very little, but after my specific research is not so, read some books, not only found the Shareperence storage value is limited, Activity and activity between the value is also limited, interested in the pot friends, can Baidu look, there are a lot of similar articles, or very grateful to those who share the technology, usually I pay little attention to these, but also to see, found really so, Gu Java Data generated by the memory is also a big piece, a lot of pot friends like to use okhttp related framework, the data parsing method really in a big piece will affect the memory, for example
1 ". Static collection classes cause memory leaks
Mainly Hashmap,vector and so on, if it is a static set of these collections are not setnull in time, they will always hold these objects.
We can also obviously see a line called Java data when we use the memory of ADB to view the research tool, in the actual view, we must have a certain understanding of json/xml and other related analytic framework
6. Memory overflow caused by unwanted resources superimposed in project compilation apk
Before I developed the Chengdu Bank mobile app, often encountered on the small mobile phone to open the apk caused by memory, we use the development tool Es,as, especially when the APK is generated by a lot of useless classes and resources, which can be caused by the function of a large amount of memory overflow, For example, when we open the mobile banking app, we need a hot update download some of the image resources will be triggered, these in the common skin-changing request app to reflect the most obvious, gu is also very important
The above is a small part of the views, no picture is more boring, if with a different view of the small, you can send mail to [email protected], together with the exchange of technology, grow together.
We are concerned about the picture animation and other issues caused by the memory problems will be in other articles to discuss with you
Android Memory leak related research "overall view"