fweigl
fweigl

Reputation: 22028

Out of Memory very quickly with Bitmaps

I have an Activity that contains a single Fragment and that Fragment has a ViewPager with a FragmentPagerAdapter that pages other Fragments:

                                         --> ChildFragment
MyActivity -> HostFragment w ViewPager   --> ChildFragment
                                         --> ChildFragment

In the ChildFragment, I use the Android-Universal-Image-Loader library to display a couple of Images. On a Nexus S i run out of memory with the infamous

java.lang.OutOfMemoryError: bitmap size exceeds VM budget

very fast, after about 10 Childfragments swiped (about 50-100 bitmaps). I tried settings all recommended settings to the UIL config, disabled chaching, to no avail. I also tried recycling all bitmaps of a Childfragment in onDestroyView, didn't help.

  1. Could my setup with the nested fragments have to do with this problem? In another app I only have an Activity with a Viewpager and the Childfragments (without the middle fragment) and I don't have this problem.

  2. I checked the Heap size in Eclipses DDMS view, how can the app crash when it still has 45% of its Heap size free? The heap view says: Heap size 10 MB Allocated 5,4 MB Free 4,5 MB How can the the app out of memory like that?

Upvotes: 0

Views: 118

Answers (3)

Manivannan
Manivannan

Reputation: 1640

this is well known issue in android.refer this. http://developer.android.com/training/displaying-bitmaps/index.html

Upvotes: 0

dumazy
dumazy

Reputation: 14435

You have to know that a Bitmap needs a lot of memory (4 bytes for each pixel) => so a 5MP photo would need 20MB if it's not shrunk.

Read the developer guide here to learn more about shrinking your bitmap to the size you need.

In short, you ask the system to retrieve the bitmap's information without retrieving the bitmap itself. With that information you calculate the inSampleSize (a certain factor to scale it) and ask the system to return the bitmap with that scale.

Upvotes: 0

Rick Falck
Rick Falck

Reputation: 1778

For one thing, you should use FragmentStatePagerAdapter. It is to be used for many Fragments and handles saving/restoring their state as you swipe between them.

Upvotes: 1

Related Questions