安卓常见引起内存泄漏的五种情况

 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;
        }
    }

}

 

posted on 2018-06-27 15:14  CallMe_HH  阅读(161)  评论(1编辑  收藏  举报

导航