简体   繁体   中英

Android first Activity not really destroyed

When I was debugging my app with AndroidDeviceMonitor, I found that my app's heap was odd. After starting the app (SplashActivity -> MainActivity), the "1-byte array" has been allocated 42MB. I was sure the SplashActivity has been destroyed and I'm using LeakCanary to discover if there is any memory leakage. But I did not find anything.

Then I try to create a new SplashActiviy, just setContentView in onCreate() method without any other code.

The layout xml is like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center">
<ImageView
    android:id="@+id/splash"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitXY"
    android:src="@drawable/splash2"/>
<ImageView
    android:id="@+id/logo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:scaleType="fitCenter"
    android:src="@drawable/splashlogo"/>
</RelativeLayout>

I debug again and the "1-byte array" is still 42MB. Then I try to remove the src tag from my layout. The "1-byte array" was reduced to 35MB. So, I guess the problem is that image resources have not been recycled. Can anyone tell me more about the detail of the first launched Activity. Why doesn't it release those resources?

What you can do to make sure that imge image is freed up for garbage collection is set the image to null in onStop to make sure that nothing has reference to it.

@Override
public void onStop() {
    super.onStop();

    mImageView.setImageDrawable(null);
}

You can then use AndroidDeviceMonitor to force a garbage collection to make sure it is freed from the heap.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM