記錄一次內(nèi)存危險操作


本文屬于裝糊涂的豬原創(chuàng),轉(zhuǎn)載請注明出處作者

背景

自定義時鐘的時候,無意間在onDraw方法中創(chuàng)建對象,然后就引發(fā)了內(nèi)存的肆意增長,偽代碼為

   @Override
    protected void onDraw(Canvas canvas) {
        method();
        invalidate();
    }

    private void method() {
        Calendar calendar = Calendar.getInstance();
        ...
    }

學(xué)習(xí)與成長

如何發(fā)現(xiàn)的呢?多虧了AndroidStudio的強大功能Android Monitor。為了更好的研究AS的這個集成功能新建了一個自定義view。

public class CurrentTv extends TextView {
    public CurrentTv(Context context) {
        this(context, null);
    }
    public CurrentTv(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    SimpleDateFormat sdf = new SimpleDateFormat( "HH:mm", Locale.getDefault());

    public CurrentTv(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
             setText(getCurrentText());
        }
    };

    public void onStart(){
        handler.removeCallbacks(renderRunnable);
        handler.post(renderRunnable);
    }

    public void onStop(){
        handler.removeCallbacks(renderRunnable);
    }


    Runnable renderRunnable = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(renderRunnable,1000);
        }
    };

    private String getCurrentText() {
        Date curDate = new Date();
        return sdf.format(curDate);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        setText(getCurrentText());
    }

    @Override
    public void invalidate() {
        if (hasWindowFocus()) {
            super.invalidate();
        }
    }


    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        if (hasWindowFocus) {
            invalidate();
        }
    }
}

很簡單的自定義view,功能是即時在Textview上顯示當前時間,但是在onDraw中創(chuàng)建的對象Date。于是乎,在下面的gif中粗淺的分析了一波。首先看到的是


image.png

Allocated不斷地增長,此時點擊


image.png

開始追蹤,幾秒后,再次點擊
image.png

停止追蹤,稍等片刻就會生成報告。

image.png

同時你可以在左側(cè)Captures中查看歷史報告

image.png

整個操作流程如下:


memory.gif

參考鏈接:
Allocation Tracker

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容