user3932494
user3932494

Reputation: 47

How to clean memory after exiting activity

As title says, I have about eight acitivies with layout full of high-res images. On weaker android devicies with low RAM memory it opens each activity alone, but when I try open another, it crashes. But when I restart app and open that activity, it works. What should I do to clean apps memory from these images from first activity to be able to open another activity? Does onDestroy() clean it?

Upvotes: 0

Views: 480

Answers (1)

Vito
Vito

Reputation: 1434

If it like resource images in xml layout, you don't need to clean up them, Android will do it for you. But if you use some big bitmaps objects.

Bitmpap bmp; // not null
bmp.recycle();
bmp = null;

final boolean bmpIsRecycled = bmp.isRecycled()
// Returns true if this bitmap has been recycled.

Free the native object associated with this bitmap, and clear the reference to the pixel data. This will not free the pixel data synchronously; it simply allows it to be garbage collected if there are no other references. The bitmap is marked as “dead”, meaning it will throw an exception if getPixels() or setPixels() is called, and will draw nothing. This operation cannot be reversed, so it should only be called if you are sure there are no further uses for the bitmap. This is an advanced call, and normally need not be called, since the normal GC process will free up this memory when there are no more references to this bitmap.

And actually when your app crashes, what error log do you have? Maybe it's not related with memory leak?

Upvotes: 2

Related Questions