0

I built an Android application which has a ViewPager with 6 pages. For every page a have a small image which I use (not background, just for an ImageView).

These images are about 130KB each. I noticed that when this activity starts I get messages like this one:

02-09 21:23:58.755: I/dalvikvm-heap(6478): Grow heap (frag case) to 49.015MB for 2457616-byte allocation

After playing a while, I also get an OOM exception.

I do not understand why an image of such size takes to much space when it is loaded.

Has anyone faced such a problem? Any solution?

Thanks in advance!

[UPDATE]

Apologies for not adding code :)

Below is the onCreateView of the Fragment I use in the ViewPager:

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ViewGroup rootView = (ViewGroup) inflater.inflate( R.layout.fragment_selection, container, false); mSelectionImage = (ImageView) rootView .findViewById(R.id.selection_image); switch (mPageNumber) { case 0: mSelectionImage.setImageResource(R.drawable.img_sel_0); break; case 1: mSelectionImage .setImageResource(R.drawable.img_sel_1); break; case 2: mSelectionImage.setImageResource(R.drawable.img_sel_2); break; case 3: mSelectionImage .setImageResource(R.drawable.img_sel_3); break; case 4: mSelectionImage.setImageResource(R.drawable.img_sel_4); break; case 5: mSelectionImage .setImageResource(R.drawable.img_sel_5); break; default: break; } return rootView; } 
5
  • This is hard to understand without code Commented Feb 9, 2013 at 21:38
  • Images take much more memory than they appear by size in png or jpg format but finally you will be asked for code in three comments below me or two because it is hard to understand memory leak in such amounts with six 130kb pngs... Commented Feb 9, 2013 at 21:42
  • are you sure the images you using are small?? Commented Feb 9, 2013 at 22:11
  • Yes, the images are 398 × 538 and their size is about 130KB each. Commented Feb 9, 2013 at 22:13
  • ok, you can try to return only one image, and see if the OOM still there. Commented Feb 10, 2013 at 9:57

1 Answer 1

1

You have 6 images , each is png , with the size of 398x538 .

This means , that by default , all of them use 6*4*398*538= 5,138,976 bytes , which is about 5 MB .

This doesn't look like the real cause of your OOM .

Could you please tell us more about the app and other things that could be the cause of this?

Is it possible you've put the files in drawable-ldpi or drawable-mdpi ?

In such cases , the images would take more , depending on the density of the device you're running.

For example , if you put them all in the drawable-mdpi , yet the device is running on xdpi screen , this would take 4 times more memory (because we double both width and height) . it's still not close to the 50MB you've got though .

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the quick feedback! The images are put in the drawable folder. Except of this, no other special activity is done. It's a simple FragmentActivity with a ViewPager and a PagerIndicator, where I have 6 fragments, as described above.
on which device/emulator do you run it on ? also , please don't ever put images in the drawable folder . put it into one of the density folders , like the drawable-xdpi or drawable-hdpi (and others) . the drawable folder is used for xml type drawables , not images . also , in order to be sure this causes the problems, please try to comment the code that you've shown here and see how much memory is used , using the ddms .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.