Andrews (Android App) performance optimization, to solve a series of problems Caton, memory overflow

In fact, online performance optimization Andrews wrote many articles, did not want to write, but suddenly saw a point not previously noticed today, with their usual also experienced a lot, so wanted to write all know performance issues all together, I will continue to update this article.

1. recycling

        Usually a lower efficiency than when accessing a property of fixed variable, if your estimate of the number of cycles often greater than 5,
the general assumption that the value is greater than 5 xxx.GetLength () method, under normal circumstances we would be so written, such as
for ( i = 0 int; i <xxx.GetLength (); i ++)
xxx.GetLength here each cycle should be called, will inevitably affect the efficiency of the program, in game development becomes even more obvious,
improved methods should be
int j = xxx.GetLength ()
for (int I = 0; I <J; I ++)  

Why write this first, because this is similar to a small point we usually hard to pay attention, when I write this is the first time I saw this point.

2. Optimization picture

        This is the largest problem I encountered at the beginning of development, the memory is the picture caused by the spill.

       A. The first is the image size of the problem, often because the artists give to our map is too large, leading to memory when loading too much for the big picture I recommend a site

https://tinypng.com/ , this site may be the picture lossless compression, the compression ratio is very high, very good. If it is some small map showing the proposed artwork for the svg, and then we converted by android studio comes with features to vector format, so you can play good optimization results.

       II. Since the picture of resource use have not been released due to memory overflow, the priority you need to look at pictures of the three-level cache architecture, in which I do not recommend you to write your own level three cache, because there is no ready-made framework for good why waste time, but certainly a lot to write their own problems, but must understand the principles involved, so I direct this framework we recommend using several images to load, following several image loading frame is now the mainstream framework, can be very effective solve problems image memory overflow, as the advantages and disadvantages of each speaker, how to use your own google it, in addition to Fresco with fewer people than the first three are used a lot.

You must learn to use a lot of good frameworks and tools, stand on the shoulders of giants you can see farther

3.String string splicing

      We often encounter string concatenation when writing code, but we most often is the way to write two strings are added, this is actually a very inefficient approach, and we actually use most of the StringBuffer and StringBuilder to stitching, the following is a comparison splicing efficiency of these three ways

String s = "";
long sBeginTime = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
     s+="s";
}
long sEndTime = System.currentTimeMillis();
System.out.println("s拼接100000遍s耗时: " + (sEndTime - sBeginTime) + "ms");
         
StringBuffer s1 = new StringBuffer();
long s1BeginTime = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
     s1.append("s");
}
long s1EndTime = System.currentTimeMillis();
System.out.println("s1拼接100000遍s耗时: " + (s1EndTime - s1BeginTime) + "ms");
         
StringBuilder s2 = new StringBuilder();
long s2BeginTime = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
    s2.append("s");
}
long s2EndTime = System.currentTimeMillis();
System.out.println("s2拼接100000遍s耗时: " + (s2EndTime - s2BeginTime) + "ms");

     

     This is the result of the test, it is clear that the use of the + string concatenation, which is significantly lower efficiency, and the use of StringBuilder StringBuffer append () method for splicing efficiency hundred or even thousands of times using the + splicing, and StringBuffer the more efficient than StringBuilder, which is due to achieve a thread-safe StringBuffer, StringBuilder little low efficiency with respect to the inevitable. Therefore, the accumulation operation of the string, the proposed combination of the thread selection problem, avoid using the + string concatenation.

4. The release unused resources

     After using the cursor sure to call cursor.close () freed;

     In addition, we used to use handler to postDelayed () perform a task, do not forget to exit, it calls handler.removeCallbacksAndMessages (null) to remove all the messages and callback functions.

 

Published 24 original articles · won praise 5 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_26923265/article/details/85340872