zoukankan      html  css  js  c++  java
  • 安卓常见引起内存泄漏的五种情况

     1,单例引起的内存泄漏----解决办法 将context生命周期和application保持一致

    public class SingletonAppliationContext {
        private static SingletonAppliationContext instance;
        private Context context;
        private SingletonAppliationContext(Context context) {
            this.context = context.getApplicationContext();// 使用Application context
        }
        public static SingletonAppliationContext getInstance(Context context) {
            if (instance == null) {
                instance = new SingletonAppliationContext(context);
            }
            return instance;
        }
    }

    2.非静态内部类创建静态实例造成的内存泄漏 解决办法将内部类noneStaticClass 设置为static
    public class StaticLeakActivity extends Activity {
        private static noneStaticClass mResource = null;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            if (mResource == null) {
                mResource = new noneStaticClass();
            }
        }
        private static class noneStaticClass {
        }
    }

    3.handler造成内存泄漏 handlerMessageMessageQueue  TLS(生命周期不一致)

    https://blog.csdn.net/u010134087/article/details/53610654
    /**
     * 解决办法:
     * 1.Handler 声明为静态的
     * 2.通过弱引用的方式引入 Activity
     */

    public class SampleActivity extends Activity {

      /**

       * Instances of static inner classes do not hold an implicit

       * reference to their outer class.

       */

      private static class MyHandler extends Handler {

        private final WeakReference<SampleActivity> mActivity;

        public MyHandler(SampleActivity activity) {

          mActivity = new WeakReference<SampleActivity>(activity);

        }

        @Override

        public void handleMessage(Message msg) {

          SampleActivity activity = mActivity.get();

          if (activity != null) {

            // ...

          }

        }

      }

      private final MyHandler mHandler = new MyHandler(this);

      /**

       * Instances of anonymous classes do not hold an implicit

       * reference to their outer class when they are "static".

       */

      private static final Runnable sRunnable = new Runnable() {

          @Override

          public void run() { /* ... */ }

      };

      @Override

      protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // Post a message and delay its execution for 10 minutes.

        mHandler.postDelayed(sRunnable, 1000 * 60 * 10);

        // Go back to the previous Activity.

        finish();

      }

    }

    4,线程造成的内存泄漏 解决办法

    /**
     * 解决办法:
     * 1.MyAsyncTask  MyRunnable

    声明为静态的
     * 2.通过弱引用的方式引入 Activity
     */

    5.Webview造成的内存泄漏

    public class WebviewLeakActivity extends AppCompatActivity {
        private WebView mWebView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mWebView = (WebView) findViewById(R.id.wv_show);
            mWebView.loadUrl("http://www.baidu.com");
        }

    // 解决办法
        @Override
        protected void onDestroy() {
            destroyWebView();
            android.os.Process.killProcess(android.os.Process.myPid());
            super.onDestroy();
        }

        private void destroyWebView() {
            if (mWebView != null) {
                mWebView.pauseTimers();
                mWebView.removeAllViews();
                mWebView.destroy();
                mWebView = null;
            }
        }

    }

  • 相关阅读:
    哈希表
    跳表
    哈夫曼之谜
    选择树、判定树和查找树

    将gbk字符串转换成utf-8,存储到注册表中后,再次从注册表读取转换成gbk,有问题!!!
    函数内部还是不要使用 strtok()
    没想到: System.out.println(n1 == f1 ? n1 : f1);
    在不同DPI屏幕环境下,让图标显示的尺寸保持不变,使用 LoadImage() 加载图标
    在多线程中显示模态窗口,出现异常现象
  • 原文地址:https://www.cnblogs.com/liheng-android/p/9234114.html
Copyright © 2011-2022 走看看