通过 Android profiler发现内存泄漏

Android studio自带的profiler具有非常强大的功能,通过它可以查看项目中是否存在内存泄漏,具体的查看方法如下

代码比较简单

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.button1:
//                跳转到没有泄露页面
                startActivity(new Intent(this, LeakNotExistActivity.class));
                break;
            case R.id.button2:
//                跳转到泄露页面
                startActivity(new Intent(this, LeakExistActivity.class));
                break;

        }

    }
}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转到没有泄露页面" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="跳转到泄露页面" />

</LinearLayout>

LeakNotExistActivity

什么都没有写不会出现内存泄漏情况

public class LeakNotExistActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leak_notexis);
    }
}

activity_leak_notexis

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

</LinearLayout>

LeakExistActivity

存在内存泄漏的文件,我们把Context 写成静态的所有杀死情况会出现内存泄漏

public class LeakExistActivity extends AppCompatActivity {
    public  static Context context;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leak_exist);
        context=this;
    }
}

我们运行程序,分别点击 button1跳转到对应页面然后返回,然后点击button2进入对应的页面然后返回此时我们点开Profiler进入下面页面

image.png

如果没有上面的页面点击+选中
image.png

image.png

如果还是没有的话那运行的时候选择


image.png

去运行项目
然后我们点击MEMORE


image.png

进入下面页面先点击1回收下可回收的内存,可以多点几下,然后点击2去分析当前内存情况


image.png

点击完后等待分析结果
选中Arrange by package


image.png

找到自己项目对象的包下看下还有哪些对象存在,如果还存在的对象是我们已经销毁的页面,那么就代码这个页面存在内存泄漏


image.png

LeakExistActivity和LeakNotExistActivity

我们都已经销毁了但是LeakExistActivity因为存在内存泄漏所以还存在内存当中。我们可以点击LeakExistActivity查看内存泄漏的信息。找到内存泄漏后具体的解决办法可以自行参考网上的代码

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容