2

I'm a little bit stumbled:

CASE 1: I tried to make an animation which consisted 26 images (each of them were about 200KB in size). Total was 5,3 MB. The animation worked okay. Images were loaded to memory and all was OK.

CASE 2: Now when I try to make an animation which holds 129 images (each around 30KB and in total 4,6 MB) I get out of memory exception.

I'm tracking memory allocation and in "case 1" the loaded bitmap image size per bitmap after loading is 691200 bytes. In "case 2" the bitmap size per file is around 563520 bytes.

used getRowBytes() * getHeight() for calculating

And that's weird, the image size in "case 2" is about 6 times smaller but after loading the sizes are quite same.

This is how i load bitmaps to memory:

       for (int i = 0; i <= 128; i++) {
            imagesHolder[i] = BitmapFactory.decodeResource(
                    getApplicationContext().getResources(),
                    getApplicationContext().getResources().getIdentifier(
                            "image" + (i + 1) + "", "drawable",
                            getApplicationContext().getPackageName()));
            System.out.println(imagesHolder[i].getRowBytes() * imagesHolder[i].getHeight());
            totalSize+=imagesHolder[i].getRowBytes() * imagesHolder[i].getHeight();
            System.out.println("TOTAL: "+totalSize);
        }

So, how come my small images get so big after loading to memory? All hints appreciated. :)

Natali
  • 2,934
  • 4
  • 39
  • 53

2 Answers2

0

This is not a precise method of determining the amount of memory needed to store your bitmaps. Every Bitmap object contains some extra fields, which also require memory. Look at this question: Android & Dalvik - Get the size of an object

Community
  • 1
  • 1
a.ch.
  • 8,285
  • 5
  • 40
  • 53
  • My calculations match the error message - E/dalvikvm-heap(2244): 563520-byte external allocation too large for this process. – user1235190 Feb 27 '12 at 10:23
0

We can develop an application size(.apk) up to 50MB memory.. There is also chance to garbage collector fail to recollect the memory. so you have to recycle the bitmap in the end; while handling no of bitmaps we have to consider the memory issues also.... if(!bmp.isRecycled()){ bmp.recycle();}

Sandeep P
  • 4,291
  • 2
  • 26
  • 45
  • I actually wan't to make a loopable live wallpaper for my phone. So i should recycle and reload images while looping the video? (video is converted to images) – user1235190 Feb 27 '12 at 10:25
  • yes. while you are using the current bitmap, recycle the bitmaps which are currently not in use. – Sandeep P Feb 27 '12 at 10:45
  • mb = milli bits...Pretty sure you mean MB = Mega Bytes. – straya Feb 27 '12 at 12:07
  • Thanks guys so far. I managed to get it working for first loop. Images got recycled and memory usage stabilized. But now i get an "Canvas.throwIfRecycled" error. It means i'm trying to reinitialize recycled bitmaps. It seems after recycling images are still present but unlinked. Is there a way to delete them from memory after showing them? – user1235190 Feb 27 '12 at 13:20
  • For a simple loop a lot of problems, guess this is not the best way to loop a video :/ – user1235190 Feb 27 '12 at 13:23