2

I have wrote a code for a button to let me move from an activity in my application to my main activity. But an error occurred during run :

07-21 09:28:13.864: E/dalvikvm-heap(371): 25165824-byte external allocation too large for this process. 07-21 09:28:13.864: E/GraphicsJNI(371): VM won't let us allocate 25165824 bytes

I have used an image and stored them in my database but without any recycle is this the problem? Here is my code in bitmap :

try {
             Log.d("to get image", "ok");
            ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
          Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageInfo.get(position)).getContent());
          imageView.setImageBitmap(bitmap);
          } catch (MalformedURLException e) {
          e.printStackTrace();
         } catch (IOException e) {
        e.printStackTrace();
        }

My app suppose to retrieve default images from youtube for any key search word and store them in my data base.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tolen
  • 111
  • 9

2 Answers2

0

This error means that You have a too large image inside. The problem here is, that You get a memory leak. If You use bitmaps, the best way is too use small bitmaps that fits on multiple devices. If You use bitmaps, also call recycle() if this bitmap is not used anymore. I recommend to read through this Guide to use Bitmaps:

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Also, there is an alternative if recycling is not possible to You. You can scale Your bitmap to Your needs like perfectly explained in another thread here in stackoverflow:

Strange out of memory issue while loading an image to a Bitmap object

Community
  • 1
  • 1
Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
0

Android apps have a limited heap size (minimum on old devices is as little as 16MB). This varies per device, so be careful during QA. Since a bitmap can take as much as 4 bytes per pixel, large bitmaps can easily fill up your heap (1000 x 1000 pixels = 3.8MB). Your bitmap seems to take 25MB which is huge!

What this means is that you have to be very careful and use several tactics to avoid wasting memory:

  1. When you don't need the bitmap anymore, release it and call bitmap.recycle() to make sure the memory is freed immediately. If you need the bitmap again in the future, you can load it again from disk/resources.

  2. If the bitmap is larger than your screen size, you can scale it down to save memory. The technique here will let you load it initially reduced by 2/3/4.. If you need more fine-tuning, you can load it full size and then rescale using Bitmap.createScaledBitmap. Just don't forget to recycle() the original large bitmap immediately.

Please note that with bitmap scaling, I did not recommend to just resize your source images. Different Android devices have different resolutions. So I would keep my source images big enough to look perfect on the highest resolution devices available. Low resolution devices usually have less heap memory, so the runtime scaling takes care of those.

talkol
  • 12,564
  • 11
  • 54
  • 64