Android内存泄漏

来源:互联网 发布:mac鼠标玩lol 编辑:程序博客网 时间:2024/06/15 07:32

1.非静态的内部类造成的内存泄漏.

由于java语言的特性,内部类会隐式的持有外部类的引用,因此当activity里面包含有非静态的内部类的时候,如果这个调用activity的finish(),这时候就会造成activity的内存泄漏. 
例子:

public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        exampleOne();    }    public void jump(View view) {        startActivity(new Intent(this, SecondActivity.class));        finish();    }    private void exampleOne() {        new MyThread().start();    }    private class MyThread extends Thread {        @Override        public void run() {            while (true) {                SystemClock.sleep(1000);            }        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

这个时候我们如果点击按钮让界面进行跳转,再结合mat工具可以分析出来内存泄漏了. 
这里写图片描述

解决方式: 
这个时候我们需要将内部类修改为静态的内部类就可以完美的解决这样的内存泄漏的问题.将代码修改为:

public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        exampleOne();    }    public void jump(View view) {        startActivity(new Intent(this, SecondActivity.class));        finish();    }    private void exampleOne() {        new MyThread().start();    }    private static class MyThread extends Thread {        @Override        public void run() {            while (true) {                SystemClock.sleep(1000);            }        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

重新运行,在结合mat进行检测就会发现内存泄漏问题被解决了. 
这里写图片描述

2.单例模式造成的内存泄漏.

我们都知道单例模式的生命周期是伴随者整个app的生命周期的.在我们使用单例的过程中如果持有了activity的引用,那么就会导致activity不能正常的销毁,由此引发内存泄漏. 
例子: 
创建一个单例:

        public class TestUitls {    private static TestUitls mTestUitls;    private Context mContext;    private TestUitls(Context context) {        this.mContext = context;    }    public static TestUitls getInstance(Context context) {        if (mTestUitls == null) {            mTestUitls = new TestUitls(context);        }        return mTestUitls;    }}        ```        然后在activity里面进行使用:        ```        public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // exampleOne();        exampleTwo();    }    private void exampleTwo() {        TestUitls.getInstance(this);    }    public void jump(View view) {        startActivity(new Intent(this, SecondActivity.class));        finish();    }    private void exampleOne() {        new MyThread().start();    }    private static class MyThread extends Thread {        @Override        public void run() {            while (true) {                SystemClock.sleep(1000);            }        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

这个时候我们如果点击按钮让界面进行跳转,再结合mat工具可以分析出来内存泄漏了. 
这里写图片描述

解决方式: 
我们知道application的context的生命周期是伴随着整个app的生命周期的,因此我们只需要传入application的context就能够解决相应的问题.

    public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // exampleOne();        exampleTwo();    }    private void exampleTwo() {        TestUitls.getInstance(this.getApplicationContext());    }    public void jump(View view) {        startActivity(new Intent(this, SecondActivity.class));        finish();    }    private void exampleOne() {        new MyThread().start();    }    private static class MyThread extends Thread {        @Override        public void run() {            while (true) {                SystemClock.sleep(1000);            }        }    }}


0 0