android2dout-of-memorypage-curl

android out of memory issue in page curl effect


in my app i am trying to show page curl effect using 100 images as explained in the following github link - here

In the sample code they are using only two images but in my app i am using around 90 images. This causes the app to get crashed, saying the following issue in logcat

E/dalvikvm-heap(603): Out of memory on a 5529616-byte allocation.

When i tried with 7 images it was working good. When i try to increase more than 7, it gets crashed. All my images are in drawable folder.

In that github code the images from drawable are taken as follows

mPages.add(BitmapFactory.decodeResource(getResources(),R.drawable.page9));
        mPages.add(BitmapFactory.decodeResource(getResources(),R.drawable.page10));
        mPages.add(BitmapFactory.decodeResource(getResources(),R.drawable.page11));
        mPages.add(BitmapFactory.decodeResource(getResources(),R.drawable.page12));
        mPages.add(BitmapFactory.decodeResource(getResources(),R.drawable.page13));
        mPages.add(BitmapFactory.decodeResource(getResources(),R.drawable.page14));
        mPages.add(BitmapFactory.decodeResource(getResources(),R.drawable.page15));

how to resolve this issue and load all the images, any better suggestions...


Solution

  • Use lazy loading mechanism. Do not load all bitmaps into memory at once, keep there id's in an ArrayList, and load them as you need them, and after using them, unload the bitmap, so that memory can be released.