Android 内存监控工具

背景

Android 内存监控工具_第1张图片
Paste_Image.png

看了微信的内存优化杂谈 看到微信做了一个内存监控的工具,定期去获取当前内存状况作相应的处理.

实现

public class MemoryWatcher extends Thread {
    private static final String TAG = "MemoryWatcher";

    private final Handler uiHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            if (msg.obj instanceof Float) {
                if (listener != null) {
                    listener.onMemorySize((Float) msg.obj);
                }
            }
        }
    };

    private Unit unit;
    private int interval;
    private InterruptionListener interruptionListener;
    private MemoryListener listener;

    @Override
    public void run() {
        setName("|MemoryWatcher|");

        while (!isInterrupted()) {

            try {
                Thread.sleep(interval);
            } catch (InterruptedException e) {
                if (interruptionListener != null) {
                    interruptionListener.onInterrupted(e);
                }
                return;
            }

            float memorySize = Utils.getSize();

            if (unit == Unit.MB) {
                memorySize = Utils.getFormatMbSize(memorySize);
            } else if (unit == Unit.KB) {
                memorySize = Utils.getFormatKbSize(memorySize);
            }

            Message msg = Message.obtain();
            msg.obj = memorySize;
            uiHandler.sendMessage(msg);
        }
    }

    public void stopWatch() {
        interrupt();
        uiHandler.removeCallbacksAndMessages(this);
    }


    public static class Builder {
        private Unit unit;
        private int interval;
        private InterruptionListener interruptionListener;
        private MemoryListener listener;


        public Builder setUnit(Unit unit) {
            this.unit = unit;
            return this;
        }

        public Builder setInterruptionListener(InterruptionListener interruptionListener) {
            this.interruptionListener = interruptionListener;
            return this;
        }

        public Builder setListener(MemoryListener listener) {
            this.listener = listener;
            return this;
        }

        public Builder setInterval(int interval) {
            this.interval = interval;
            return this;
        }

        public MemoryWatcher build() {
            return new MemoryWatcher(this);
        }
    }

    private MemoryWatcher(Builder builder) {
        this.unit = builder.unit;

        if (this.unit == null) {
            this.unit = Unit.MB;
        }
        this.interval = builder.interval;

        if (this.interval == 0) {
            this.interval = Const.DEFAULT_INTERVAL;
        }

        this.listener = builder.listener;
        this.interruptionListener = builder.interruptionListener;
    }
}

代码还是比较简单的 启动一个线程在后台一直跑,固定时间休眠,然后检测当前内存使用大小,然后回调给UI线程.

使用

public class MainApplication extends Application {

    private static final String TAG = "EvilsoulM";

    private MemoryWatcher memoryWatcher;

    @Override
    public void onCreate() {
        super.onCreate();
        initMemoryWatcher();
    }

    private void initMemoryWatcher() {
        MemoryWatcher.Builder builder = new MemoryWatcher.Builder().setInterval(5 * 1000).setListener(new MemoryListener() {
            @Override
            public void onMemorySize(float size) {
                Log.i(TAG, "size->" + size);
                if (size > 100) {//eg 大于100MB内存
                    //TODO clear memory cache
                }
            }
        }).setUnit(Unit.MB);

        memoryWatcher = builder.build();
        memoryWatcher.start();
    }


    @Override
    public void onTerminate() {
        super.onTerminate();
        Log.d(TAG, "onTerminate() called with: " + "");
        memoryWatcher.stopWatch();
        memoryWatcher = null;
    }
}

Demo->https://github.com/EvilsoulM/MemoryWatcher

你可能感兴趣的:(Android 内存监控工具)