Android想尽一切办法进行优化,争取每一个点都做实验,不断更新

下面的各个优化都是从各个地方收集过来的,争取每一个都做一个小实验,到底会节约多少时间,节约多少内存等...

1.永远不要在for的第二个条件中调用任何方法
for(int i =0; i < mList.size(); i++) {}
应该改成
int size = mList.size();
for(int i =0; i < size; i++) {}
后者不会多次调用mList.size()时间会减少多少呢?
代码来说话:

private void testFoo() {
       ArrayList<String> mList = new ArrayList<String>();
       
       for (int i = 0 ; i < 100 ; i++){
           mList.add("test");
       }
       
       
       
       for (int i = 0 ; i < TEST_TIME ; i++){ // 实验十次
            long time1 = System.currentTimeMillis();
            for (int j = 0; j < mList.size(); j++) {
                test();
            }
            long time2 = System.currentTimeMillis();
            
            long time3 = System.currentTimeMillis();
            int size = mList.size();
            for (int j = 0; j < size; j++) {
                test();
            }
            long time4 = System.currentTimeMillis();
            Log.i(TAG , "Time : " + i + " FirstTime = " + (time2 - time1) + " SecondTime = " + 
            (time4 - time3));
            
        }
        
    }
<span style="white-space:pre">	</span>private void test(){
	   int i = 10;
	   int j = 20;
	   int sum = i+ j;
	}
这个实验结果是 前后2个时差不多的:

基本没有差异,那么就把mList的大少改成10000,那结果是什么样的呢?

修改代码:为10000次

for (int i = 0 ; i < 10000 ; i++){
           mList.add("test");
       }
实验结果是:


实验结果是使用后者时间明显减半。所有尽量使用后面一种方法!


2.访问成员变量比访问本地变量慢得多,如下面一段代码:
for(int i =0; i < this.mCount; i++)  {}  
应该改成
int count = this.mCount
for(int i =0; i < count; i++)  {}  


增加成员变量:

int mCount = 100;

实验代码:

 for (int i = 0 ; i < TEST_TIME ; i++){ // 实验十次
            long time1 = System.currentTimeMillis();
            for (int j = 0; j < this.mCount; j++) {
                test();
            }
            long time2 = System.currentTimeMillis();
            
            long time3 = System.currentTimeMillis();
            int size = mCount;
            for (int j = 0; j < size; j++) {
                test();
            }
            long time4 = System.currentTimeMillis();
            Log.i(TAG , "Time : " + i + " FirstTime = " + (time2 - time1) + " SecondTime = " + 
            (time4 - time3));
            
        }
显示100次是结果:

然后int mCount = 10000;

实现结果为:


还是不明显,继续增加次数

然后int mCount = 1000000;

实验结果为:


这个结果可以看出,后面的方法被前面的方法使用的时间要少

只是访问一个变量而已时间就会少一点,我们的程序有那么的代码,各种访问,加起来时间就多了。

对于最后一次的实验结果,我表示很惊讶, 但是这不是这里讨论的问题。


3. for 跟 for-each那个会快一些:

实验代码:

int[] array = new int[1000];
	   int sum = 0;
       for (int i = 0 ; i < TEST_TIME ; i++){ // 实验十次
            long time1 = System.currentTimeMillis();
            for (int j : array) {
                test();
            }
            long time2 = System.currentTimeMillis();
            
            long time3 = System.currentTimeMillis();
            int size = array.length;
            for (int j = 0; j < size; j++) {
                test();
            }
            long time4 = System.currentTimeMillis();
            Log.i(TAG , "Time : " + i + " FirstTime = " + (time2 - time1) + " SecondTime = " + 
            (time4 - time3));
            
        }

先使用1000次,使用结果:


再看100000次的结果:


再看10000000的结果:

由此可以看  for语句 比  for-each的运行要快一点:

在java1.5中引入的for-each语法。编译器会将对数组的引用和数组的长度保存到本地变量中,这对访问数组元素非常好。 但是编译器还会在每次循环中产生一个额外的对本地变量的存储操作(如下面例子中的变量a),这样会比普通循环多出4个字节


4.避免使用枚举

枚举变量非常方便,但不幸的是它会牺牲执行的速度和并大幅增加文件体积。
使用枚举变量可以让你的API更出色,并能提供编译时的检查。
所以在通常的时候你毫无疑问应该为公共API选择枚举变量。但是当性能方面有所限制的时候,你就应该避免这种做法了。


5.避免使用浮点数
通常的经验是,在Android设备中,浮点数会比整型慢两倍。
Android是基于Linux的,同样对浮点数的支持并不是很好!

实验代码:
for (int i = 0 ; i < TEST_TIME ; i++){ // 实验十次
            long time1 = System.currentTimeMillis();
            for (int j = 0; j < 1000000; j++) {
               float k = 1.0f + j;
            }
            long time2 = System.currentTimeMillis();
            
            long time3 = System.currentTimeMillis();
            for (int j = 0; j < 1000000; j++) {
                    int k = 1 + j;
            }
            
            long time4 = System.currentTimeMillis();
            Log.i(TAG , "Time : " + i + " FirstTime = " + (time2 - time1) + " SecondTime = " + 
            (time4 - time3));
            
        }
结果:

 

6.使用实体类比接口好
假设你有一个HashMap对象,你可以将它声明为HashMap或者Map:
Map map1 = new HashMap();
HashMap map2 = new HashMap();
哪个更好呢?

按照传统的观点Map会更好些,因为这样你可以改变他的具体实现类,只要这个类继承自Map接口。
传统的观点对于传统的程序是正确的,但是它并不适合嵌入式系统。
调用一个接口的引用会比调用实体类的引用多花费一倍的时间。如果HashMap完全适合你的程序,那么使用Map就没有什么价值。
如果有些地方你不能确定,先避免使用Map,剩下的交给IDE提供的重构功能好了。
(当然公共API是一个例外:一个好的API常常会牺牲一些性能)


对于以上观点我做了一下实验 ,发现实验结果跟上面说的有点不一样:

 Map<Integer , String> map1 = new HashMap<Integer, String>();
	    HashMap<Integer , String> map2 = new HashMap<Integer, String>();
	    
        
        for (int i = 0 ; i < TEST_TIME ; i++){ // 实验十次
            long time1 = System.currentTimeMillis();
            for (int j = 0; j < 10000; j++) {
                map1.put(j, "" + j);
            }
            long time2 = System.currentTimeMillis();
            
            long time3 = System.currentTimeMillis();
            for (int j = 0; j < 10000; j++) {
                map2.put(j, "" + j);
            }
            
            long time4 = System.currentTimeMillis();
            Log.i(TAG , "Time : " + i + " FirstTime = " + (time2 - time1) + " SecondTime = " + 
            (time4 - time3));
            
        }

这里的 TEST_TIME设置为100,结果为:


这单单是从put运行的效果来说的,接下再进行get操作的实验结果如下:


7.对常量使用static final修饰符
让我们来看看这两段在类前面的声明:
static int intVal = 42;
static String strVal = "Hello, world!";
编译器会生成一个叫做clinit的初始化类的方法,当类第一次被使用的时候这个方法会被执行。方法会将42赋给intVal,然后把一个指向类中常量表 的引用赋给strVal。当以后要用到这些值的时候,会在成员变量表中查找到他们。 下面我们做些改进,使用“final”关键字:
static final int intVal = 42;
static final String strVal = "Hello, world!";
现在,类不再需要clinit方法,因为在成员变量初始化的时候,会将常量直接保存到类文件中。用到intVal的代码被直接替换成42,而使用strVal的会指向一个字符串常量,而不是使用成员变量。
将一个方法或类声明为final不会带来性能的提升,但是会帮助编译器优化代码。举例说,如果编译器知道一个getter方法不会被重载,那么编译器会对其采用内联调用。
你也可以将本地变量声明为final,同样,这也不会带来性能的提升。使用“final”只能使本地变量看起来更清晰些(但是也有些时候这是必须的,比如在使用匿名内部类的时候)。




8.静态方法代替虚拟方法
如果不需要访问某对象的字段,将方法设置为静态,调用会加速15%到20%。
这也是一种好的做法,因为你可以从方法声明中看出调用该方法不需要更新此对象的状态。

前提是不需要访问对象的字段:

比较方法

public class Utils {
    
    public static void add(int i , int j){
        /**
         * doNothing
         */
    }
    
    public void add1(int i , int j){
        /**
         *  doNothing
         */
    }
}
静态的方法会好一些:

Utils mUtils = new Utils();
        for (int i = 0 ; i < TEST_TIME ; i++){ // 实验十次
            long time1 = System.currentTimeMillis();
            for (int j = 0; j < 1000000; j++) {
                mUtils.add1(i, j);
                
            }
            long time2 = System.currentTimeMillis();
            
            long time3 = System.currentTimeMillis();
            for (int j = 0; j < 1000000; j++) {
                Utils.add(i, j);
            }
            
            long time4 = System.currentTimeMillis();
            Log.i(TAG , "Time : " + i + " FirstTime = " + (time2 - time1) + " SecondTime = " + 
            (time4 - time3));
            
        }

实验结果为:


9.减少不必要的全局变量

尽量避免static成员变量引用资源耗费过多的实例,比如Context

</pre><p></p><div class="dp-highlighter bg_java" style="font-family: Consolas, 'Courier New', Courier, mono, serif; width: 700.90625px; overflow: auto; padding-top: 1px; color: rgb(51, 51, 51); line-height: 26px; margin: 18px 0px !important; background-color: rgb(231, 229, 220);"><ol start="1" class="dp-j" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important; background-color: rgb(255, 255, 255);"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">private</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">static</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Drawable sBackground;  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="annotation" style="margin: 0px; padding: 0px; border: none; color: rgb(100, 100, 100); background-color: inherit;">@Override</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">protected</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> onCreate(Bundle state) {  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">super</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">.onCreate(state);  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  TextView label = <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> TextView(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">this</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  label.setText(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"Leaks are bad"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">if</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> (sBackground == </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">null</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">) {  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    sBackground = getDrawable(R.drawable.large_bitmap);  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  }  </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  label.setBackgroundDrawable(sBackground);  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  setContentView(label);  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">}  </span></li></ol></div><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">    这段代码效率很快,但同时又是极其错误的; </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">    在第一次屏幕方向切换时它泄露了一开始创建的Activity。当一个Drawable附加到一个 View上时, </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">    View会将其作为一个callback设定到Drawable上。上述的代码片段,意味着Drawable拥有一个TextView的引用,</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">    而TextView又拥有Activity(Context类型)的引用,换句话说,Drawable拥有了更多的对象引用。即使Activity被 销毁,内存仍然不会被释放。 </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">    另外,对Context的引用超过它本身的生命周期,也会导致Context泄漏。所以尽量使用Application这种Context类型。 </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">    这种Context拥有和应用程序一样长的生命周期,并且不依赖Activity的生命周期。如果你打算保存一个长时间的对象, </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">    并且其需要一个 Context,记得使用Application对象。你可以通过调用Context.getApplicationContext()或 Activity.getApplication()轻松得到Application对象。 </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">    最近遇到一种情况引起了Context泄漏,就是在Activity销毁时,里面有其他线程没有停。 </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">    总结一下避免Context泄漏应该注意的问题: </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">    1.使用Application这种Context类型。 </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">    2.注意对Context的引用不要超过它本身的生命周期。 </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">    3.慎重的使用“static”关键字。 </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">    4.Context里如果有线程,一定要在onDestroy()里及时停掉。</span><div class="column" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></div><div class="column" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">10.<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:21px"><span style="margin:0px; padding:0px"><span style="margin:0px; padding:0px"><strong><span style="font-family:'Microsoft YaHei'; font-size:18px">避免创建不必要的对象</span></strong></span></span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; margin:10px auto; padding-top:0px; padding-bottom:0px"><span style="margin:0px; padding:0px">最常见的例子就是当你要频繁操作一个字符串时,使用StringBuffer代替String。</span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; margin:10px auto; padding-top:0px; padding-bottom:0px"><span style="margin:0px; padding:0px">对于所有所有基本类型的组合:int数组比Integer数组好,这也概括了一个基本事实,两个平行的int数组比</span> (<span style="margin:0px; padding:0px">int</span>,<span style="margin:0px; padding:0px">int</span>)<span style="margin:0px; padding:0px">对象数组性能要好很多。</span></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; margin:10px auto; padding-top:0px; padding-bottom:0px">总体来说,就是避免创建短命的临时对象。减少对象的创建就能减少垃圾收集,进而减少对用户体验的影响。</p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; margin:10px auto; padding-top:0px; padding-bottom:0px">11.<span style="font-family:'Microsoft YaHei'; font-size:18px"><strong>避免内部Getters/Setters</strong></span></p><div class="column" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">在Android中,虚方法调用的代价比直接字段访问高昂许多。通常根据面向对象语言的实践,在公共接口中使用Getters和Setters是有道理的,但在一个字段经常被访问的类中宜采用直接访问。</div><div class="column" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">12.BaseAdapter 跟 ViewHolder 优化</div><div class="column" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">提供自定义的Adapter模板:</div><div class="column" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><pre name="code" class="java">public class MyPhoteDirAdapter extends BaseAdapter{
    Context mContext = null;
    ArrayList<E> mList = null;
    
    public MyPhoteDirAdapter(ArrayList<E> list , Context context){
        mList = list;
        mContext = context;
    }
    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int arg0) {
        return mList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }
    
     ViewHolder mHolder = null;
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder mHolder = null;
        if (null == view){
            view = LayoutInflater.from(mContext).inflate(R.layout.layout_photodir, null);
            mHolder = new ViewHolder();
            mHolder.mImage = (ImageView)view.findViewById(R.id.photo_thumb);
            mHolder.mText = (TextView)view.findViewById(R.id.photo_name);
            view.setTag(mHolder);
        }else{
            mHolder = (ViewHolder)view.getTag();
        }
        mHolder.mImage.setImageDrawable(mList.get(position).getmThumb());
        mHolder.mText.setText(mList.get(position).getName());
        return view;
    }
    
    public class ViewHolder{
        public ImageView mImage = null;
        public TextView mText = null;
    }

}
12.慎重使用<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">static关键字</span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">    当类的成员变量声明成static后,它是属于类的而不是属于对象的,如果我们将很大的资源对象(Bitmap,context等)声明成static,那么这些资源不会随着对象的回收而回收, </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">    会一直存在,所以在使用static关键字定义成员变量的时候要慎重。</span>
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">13.<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">Bitmap使用后未调用recycle()。</span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">    图片处理不好是造成内存溢出的又一个头号原因,(在我们的产品中也有体现), </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">    当我们处理完图片之后可以通过调用recycle()方法来回收图片对象 </span></span><div class="dp-highlighter bg_java" style="font-family: Consolas, 'Courier New', Courier, mono, serif; width: 700.90625px; overflow: auto; padding-top: 1px; color: rgb(51, 51, 51); line-height: 26px; margin: 18px 0px !important; background-color: rgb(231, 229, 220);"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(108, 226, 108); background-color: rgb(248, 248, 248);"><strong>[java]</strong> <a target=_blank href="http://blog.csdn.net/TomHelper/article/details/24727657#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">view plain</a><a target=_blank href="http://blog.csdn.net/TomHelper/article/details/24727657#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_copy.gif); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">copy</a><a target=_blank href="https://code.csdn.net/snippets/319115" target="_blank" title="在CODE上查看代码片" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="border: none; max-width: 100%; position: relative; top: 1px; left: 2px;" /></a><a target=_blank href="https://code.csdn.net/snippets/319115/fork" target="_blank" title="派生到我的代码片" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="border: none; max-width: 100%; position: relative; top: 2px; left: 2px;" /></a><div style="position: absolute; left: 834px; top: 2933px; width: 18px; height: 18px; z-index: 99;"></div></div></div><ol start="1" class="dp-j" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important; background-color: rgb(255, 255, 255);"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">if</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(!bitmap.isRecycled())  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">{  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    bitmap.recycle()  </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">}          </span></li></ol></div><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">    除此之外: </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">    直接使用ImageView显示bitmap会占用较多资源,特别是图片较大的时候,可能导致崩溃。 </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">    使用BitmapFactory.Options设置inSampleSize, 这样做可以减少对系统资源的要求。 </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">    属性值inSampleSize表示缩略图大小为原始图片大小的几分之一,即如果这个值为2,则取出的缩略图的宽和高都是原始图片的1/2,图片大小就为原始大小的1/4。 </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">        BitmapFactory.Options bitmapFactoryOptions = new BitmapFactory.Options();  </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">        bitmapFactoryOptions.inJustDecodeBounds = true;  </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">        bitmapFactoryOptions.inSampleSize = 2;  </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">        // 这里一定要将其设置回false,因为之前我们将其设置成了true  </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">        // 设置inJustDecodeBounds为true后,decodeFile并不分配空间,即,BitmapFactory解码出来的Bitmap为Null,但可计算出原始图片的长度和宽度  </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">        options.inJustDecodeBounds = false; </span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">        Bitmap bmp = BitmapFactory.decodeFile(sourceBitmap, options);  </span>
14.open/close  register/unRegister成对出现 , 及时对资源进行回收,释放
调用registerReceiver()后要调用unregisterReceiver()
finally 里面 关闭InputStream/OutputStream
当我们操作完数据库后,一定要记得调用Cursor对象的close()来关闭游标,释放资源。 

15. 了解Java四种引用方式

  JDK 1.2版本开始,把对象的引用分为4种级别,从而使程序能更加灵活地控制对象的生命周期。这4种级别由高到低依次为:强引用、软引用、弱引用和虚引用。

                     i.    强引用(StrongReference)

         强引用是使用最普遍的引用。如果一个对象具有强引用,那垃圾回收器绝不会回收它。当内存空间不足,Java虚拟机宁愿抛出OutOfMemoryError错误,使程序异常终止,也不会靠随意回收具有强引用的对象来解决内存不足的问题。

 

                     ii.    软引用(SoftReference)

         如果一个对象只具有软引用,则内存空间足够,垃圾回收器就不会回收它;如果内存空间不足了,就会回收这些对象的内存。只要垃圾回收器没有回收它,该对象就可以被程序使用。软引用可用来实现内存敏感的高速缓存。

 

                   iii.    弱引用(WeakReference)

        在垃圾回收器线程扫描它所管辖的内存区域的过程中,一旦发现了只具有弱引用的对象,不管当前内存空间足够与否,都会回收它的内存。不过,由于垃圾回收器是一个优先级很低的线程,因此不一定会很快发现那些只具有弱引用的对象。

 

                    iv.    虚引用(PhantomReference)

        顾名思义,就是形同虚设。与其他几种引用都不同,虚引用并不会决定对象的生命周期。如果一个对象仅持有虚引用,那么它就和没有任何引用一样,在任何时候都可能被垃圾回收器回收。了解并熟练掌握这4中引用方式,选择合适的对象应用方式,对内存的回收是很有帮助的。

详细请参考 http://blog.csdn.net/feng88724/article/details/6590064

16.

了解并使用类库

  选择Library中的代码而非自己重写,除了通常的那些原因外,考虑到系统空闲时会用汇编代码调用来替代library方法,这可能比JIT中生成的等价的最好的Java代码还要好。

           i.    当你在处理字串的时候,不要吝惜使用String.indexOf(),String.lastIndexOf()等特殊实现的方法。这些方法都是使用C/C++实现的,比起Java循环快10到100倍。

           ii.    System.arraycopy方法在有JIT的Nexus One上,自行编码的循环快9倍。

           iii.    android.text.format包下的Formatter类,提供了IP地址转换、文件大小转换等方法;DateFormat类,提供了各种时间转换,都是非常高效的方法。

      详细请参考 http://developer.android.com/reference/android/text/format/package-summary.html

           iv.    TextUtils类

      对于字符串处理Android为我们提供了一个简单实用的TextUtils类,如果处理比较简单的内容不用去思考正则表达式不妨试试这个在android.text.TextUtils的类,详细请参考http://developer.android.com/reference/android/text/TextUtils.html

 

            v.    高性能MemoryFile类。

       很多人抱怨Android处理底层I/O性能不是很理想,如果不想使用NDK则可以通过MemoryFile类实现高性能的文件读写操作。MemoryFile适用于哪些地方呢?对于I/O需要频繁操作的,主要是和外部存储相关的I/O操作,MemoryFile通过将 NAND或SD卡上的文件,分段映射到内存中进行修改处理,这样就用高速的RAM代替了ROM或SD卡,性能自然提高不少,对于Android手机而言同时还减少了电量消耗。该类实现的功能不是很多,直接从Object上继承,通过JNI的方式直接在C底层执行。

详细请参考 http://developer.android.com/reference/android/os/MemoryFile.html

在此,只简单列举几个常用的类和方法,更多的是要靠平时的积累和发现。多阅读Google给的帮助文档时很有益的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值