Android内存优化实列分析

内存泄露

  • 静态对象引用Context
//静态View导致内存泄露
 private static TextView sTextView;

 protected void onCreate(Bundle savedInstanceState) {
        ....
        sTextView = (TextView) findViewById(R.id.tv_text);
        ....
        }

从上面的代码不能直接看出ViewContext的引用,但从View构造器可知创建View必须引用Context(源码太复杂了没有找到相关代码,另外View是依赖于Activity的,其生命周期一般不会高于Activity,声明为static是完全没有必要的这里只是用来说明静态对象引用可能会导致内存泄露

View(Context context)

View(Context context, AttributeSet attrs)

View(Context context, AttributeSet attrs, int defStyleAttr)

View(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

总结:
尽量减少静态对象的引用,如果一定要引用可以使用弱引用WeakReference

修改后的代码

private TextView sTextView;
....
  • 内部类持有外部对象
    //Handler.Callback内部类默认持有外部对象`MainActivity`
    Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            return false;
        }
    });
    ....
    mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                System.out.println("post delayed may leak");
            }
        }, 50000);
    ....

上面代码中Handler.CallbackRunnable都是匿名内部类,非静态的内部类是默认持有外部对象的,所以Handler.CallbackRunnable都持有MainActivity的引用。Handler的对象是与当前线程绑定的,生命周期要比MainActivity长,而Handler通过Handler.CallbackRunnable间接持有MainActivity使得MainActivity无法通过GC清除(比如当屏幕旋转时会创建新的MainActivity对象而原来的MainActivity对象由于被Handler间接引用无法通过GC清除从而出现内存泄露)

修改后的代码

    Handler mHandler = new Handler(new HandlerCallback());
    //静态内部类不持有外部对象
    static class HandlerCallback implements Handler.Callback{
        @Override
        public boolean handleMessage(Message msg) {
            return false;
        }
    }
    ....
    mHandler.postDelayed(new MyRunnable(), 5000);
    ....
     //静态内部类不持有外部对象
    static class MyRunnable implements Runnable{

        @Override
        public void run() {
            System.out.println("post delayed may leak");
        }
    }

内存抖动

短时建创建大量对象又迅速被销毁,频繁触发GC造成程序卡顿用户体验下降

 ....
for (int i = 0; i < 10000; i++) {
            Rect rect = new Rect(0, 0, 100, 100);
            System.out.println("-------: " + rect.width());
        }

 ....
  @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //避免在onDraw等调用频繁的方法中创建对象
        RectF rect =  new RectF(0, 0, 100, 100);
        Paint paint = new Paint();

 ....

避免在调用频繁的方法中创建对象

修改后的代码

    private Rect rect = new Rect(0, 0, 100, 100);
    ....

     for (int i = 0; i < 10000; i++) {
            //避免短时间内创建大量对象产生内存抖动
           // Rect rect = new Rect(0, 0, 100, 100);
            System.out.println("-------: " + rect.width());
        }
    ....

    private RectF rect =  new RectF(0, 0, 100, 100);
    private Paint paint = new Paint();

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    ....
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值