Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory Leak on Orientation Change

I've written a pretty standard Android app that displays a bunch of pictures (from Contacts) in a GridView. The app doesn't do anything special to try to retain data on screen orientation changes, and just recreates the GridView, adapter and loader when the Activity is recreated.

However, after a few orientation changes, the app slows down; after a few more, it crashes with an out-of-memory error (at BitmapFactory.decodeStream()). This still happens if I leave it sit for a minute between rotations to let the garbage collector do its thing.

I was under the impression that Android would free all memory associated with an Activity when the Acitivty is destroyed during orientation changes. However, this seems not to be the case. My question is: what memory could I be inadvertently retaining despite Activity destruction?

(Note that the app runs fine so long as it is not subjected to too many orientation changes, so the general approaches to memory minimisation that I'm using are sufficient.)

like image 589
Peter McLennan Avatar asked Jan 27 '26 11:01

Peter McLennan


1 Answers

I think you forgot add bitmap.recycle();

Also easy method fix this, add to AndroidManifest, activity parameter: android:configChanges="orientation|screenSize"

like image 50
artemiygrn Avatar answered Jan 28 '26 23:01

artemiygrn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!