Android开发实践:检测App的内存占用和泄漏

http://www.linuxidc.com/Linux/2014-03/97563.htm


官方文档对于内存管理

http://developer.android.com/intl/zh-cn/training/articles/memory.html

http://blog.csdn.net/hknock/article/details/47322005  (中文翻译)


Android代码内存优化建议-Android资源篇

http://blog.csdn.net/hknock/article/details/47322005


https://cloud.tencent.com/developer/article/1070616  (一招内存优化)


Android 内存管理 &Memory Leak & OOM 分析

http://blog.csdn.net/vshuang/article/details/39647167



ANDROID 探究oom内幕

http://blog.csdn.net/chengyingzhilian/article/details/8662849


Android DiskLruCache缓存完全解析

http://blog.csdn.net/lwyygydx/article/details/40401211


Java内存分配全面浅析

 http://blog.csdn.net/yangyuankp/article/details/7651251


Java内存分配、管理小结

 http://java-mzd.iteye.com/blog/848635


JVM 的 工作原理,层次结构 以及 GC工作原理

 http://segmentfault.com/a/1190000002579346  


  • 内存监听


    如何制造OOM?

    不要用while循环来制造oom,不易于控制,容易崩溃了,onTrimMemory方法也就没有回调了。看我下面的有个make(int count)方法,如果onTrimMemory没有回调,就多调用几次。

  • package com.mh.webappStart.util;
    
    import android.graphics.BitmapFactory;
    import android.os.Environment;
    import android.os.SystemClock;
    import com.gen.mh.webapps.utils.Logger;
    
    import com.gen.mh.webapps.WebViewFragment;
    import com.gen.mh.webapps.utils.Logger;
    import com.mh.webappStart.WebApplication;
    
    import java.io.File;
    
    public class OOMMaker {
        private static final String TAG = "OOMMaker";
        private static boolean isStop = false;
    
        public static  void make(){
            isStop = false;
            while(isStop == false){
                Logger.e(TAG, "onTrimMemory test " );
                new Thread(){
                    @Override
                    public void run() {
                        WebViewFragment webViewFragment1 = new WebViewFragment();
                        WebViewFragment webViewFragment2 = new WebViewFragment();
                        WebViewFragment webViewFragment3 = new WebViewFragment();
                        WebViewFragment webViewFragment4 = new WebViewFragment();
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inJustDecodeBounds = false;
                        options.inSampleSize = 1; // 原图的五分之一,设置为2则为二分之一
                        BitmapFactory.decodeFile(WebApplication.getInstance().getApplication().getFilesDir() + File.separator
                                + "app/api_plugin/images"
                                + File.separator + "oom_test.jpg", options);
                    }
                }.start();
    
            }
            Logger.e("stop make oom ...");
        }
    
        public static  void make(int count){
            for (int i = 0; i < count; i++) {
                Logger.e(TAG, "onTrimMemory test " + i );
                new Thread(){
                    @Override
                    public void run() {
                        WebViewFragment webViewFragment1 = new WebViewFragment();
                        WebViewFragment webViewFragment2 = new WebViewFragment();
                        WebViewFragment webViewFragment3 = new WebViewFragment();
                        WebViewFragment webViewFragment4 = new WebViewFragment();
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inJustDecodeBounds = false;
                        options.inSampleSize = 1; // 原图的五分之一,设置为2则为二分之一
                        BitmapFactory.decodeFile(WebApplication.getInstance().getApplication().getFilesDir() + File.separator
                                + "app/api_plugin/images"
                                + File.separator + "oom_test.jpg", options);
                    }
                }.start();
            }
            Logger.e("stop make oom ...");
        }
    
    
        public static  void stop(){
            Logger.e("stop make oom2 ...");
            isStop = true;
        }
    }



前些日子,一位面试官(乔丽云,大约30来岁,个子不是很高),我从她那里确实领会了一些东西:

  1. 假如有这样一个需求,判断一个文件的格式

    一般的人会将格式全部存入HashMap当中,然后去遍历,然后问我有没有什么优化的方法,就是不写

    入到内存当中?

    答案:用if else或switch来用语法来判断,这样是节省了内存,但是在性能上可能上会有一定的代价。

  2. 在Activity退出,注销登录时,如果还有网络请求在运行,应该怎么取消?

    Volley库可以做到

  3. 对于一些图片库,在下载完图片时,如果任务在进行,而界面退出了,应该怎么取消?图片库做了

    什么处理?


DiskLruCache详解

http://blog.csdn.net/lwyygydx/article/details/40401211