Skip to content Skip to sidebar Skip to footer

Release Memory Of Particular Activity When It Is Destroyed

I have a launcher Activity that load and resize big bitmap as it's background when it opens. Whenever hit the back button, the Activity is destroyed. But I think the memory is not

Solution 1:

Add following code for it

@OverrideprotectedvoidonDestroy() {
    //android.os.Process.killProcess(android.os.Process.myPid());super.onDestroy();
    if(scaledBitmap!=null)
            {
                scaledBitmap.recycle();
                scaledBitmap=null;
            }

     }

Solution 2:

In activity if you're calling the finish() method from is destroyed and all its resources are queued for garbage collection.

So, all memory that was used by this activity will be freed during next GC cycle.

OR

you can try this to clean memory,

@Override
public void onDestroy() {
    super.onDestroy();
    Runtime.getRuntime().gc();      
}

check this details. hope that helps.

Solution 3:

Try to set the bitmap to null while activity is destroyed and if desire run the garbage collector.

Solution 4:

add this to your code

@OverridepublicbooleanonKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK))
    {
        finish();
    }
    returnsuper.onKeyDown(keyCode, event);
}

Solution 5:

even after destroying the activity by calling finish(), it's resources are queued for garbage collection. activity will be freed during next GC cycle.

@Override
public void onDestroy() {
    super.onDestroy();
    Runtime.getRuntime().gc();      
}

You can also use android:largeHeap="true" to request a larger heap size, in your application tag in manifest.

Post a Comment for "Release Memory Of Particular Activity When It Is Destroyed"