Peter McLennan
Peter McLennan

Reputation: 503

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.)

Upvotes: 3

Views: 3035

Answers (1)

artemiygrn
artemiygrn

Reputation: 1036

I think you forgot add bitmap.recycle();

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

Upvotes: 1

Related Questions