0

I know there is plenty of questions already resolved with the same name as mine, but none of them gave me an answer. Here is what I do :

I have a big multidimensionnal array containing items, each with name, image path, and other stuff. I have a ListView listing all these items from that array, using their names, and when we click on one of these, it opens a new Activity showing the details of the item, and the image. It actually works, the image is well scaled, all loaded things match with the user's click, this part is 100% ok.

But after several clicks, I got OutOfMemory exception. In my previous version of the app, there was no images, and I could click on all the items in a row (almost a hundred items), without any trouble. Now there is images, and out of memory exception. So I assume that images are the problem. Here is how I do to dynamically select an image after a click :

ItemArray items = new ItemArray(itemID); //clicked item ID to match with the array
        setContentView(R.layout.activity_show_item);
        ImageView itemImage = (ImageView) findViewById(R.id.itemImageView);
        Resources res = getResources();
        int resID = res.getIdentifier(items.ArrayToPrint[1], "drawable", getPackageName()); //ArrayToPrint = the selected lines matching the item, verification is made in the ItemArray class and is working properly
        itemImage.setImageResource(resID);

With this code, the image appears as I want it to. But as I said earlier, I got OutOfMemory exception. I tried several things to flush caches, but none worked, I still got this problem, and don't know how to deal with it.

Any clues ? Thanks in advance !

YumeYume
  • 981
  • 2
  • 12
  • 33
  • when you say `after several click` what are you doing in those clicks. also what is the size of the image you are displaying – tyczj Aug 08 '14 at 17:23
  • When I click on an item in the list, I open a new activity, passing the selected item ID in parameter, and then, in this new activity, I'm doing what the code above is meant to, which is get and display the proper image. They are 1000*1000 PNG images, but none of them is bigger than 50kb. I tried with smaller images, then making them bigger to fit with the screen width, but text written on these images are unreadable, this is why I took larger pictures. – YumeYume Aug 08 '14 at 18:53
  • file size does not mean much, when you load an image with dimensions of 1000*1000 that image is taking up a total of almost `4mb` of memory so if you are loading multiple 1000*1000 images into memory at a time you can run out of memory "after several clicks" plus `PNG` images require more memory too – tyczj Aug 08 '14 at 18:57
  • Yeah that's what I thought. Images are ALL called by the same ImageView, and only one at a time, then the user click on Back, and can select another item. With this, there's no way to immediately clean the memory before loading a new image into the ImageView ? – YumeYume Aug 08 '14 at 19:04
  • is this in a listview or something? – tyczj Aug 08 '14 at 19:07
  • either way you should not be putting the entire 1000*1000 image into the imageview and you should look at this http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object/823966#823966 – tyczj Aug 08 '14 at 19:13
  • I got a ListActivity, where is a ListView showing all item names. When I click on one of these items, it starts a new activity, showing the picture related to the clicked item. At this point, there is nothing more to do than going back to the previous activity. Then, if I click on another item, it open the image activity again, showing this new image, and only this one. That's why I thought there was a way to completely reset the ImageView memory, because only one image is loaded at a time, never more. – YumeYume Aug 08 '14 at 19:21
  • when you leave an activity the image should be garbage collected but you can always set the imageview bitmap to null. you still should look at the link I gave you however – tyczj Aug 08 '14 at 19:23
  • I just tested everything. Tried to put back the imageview to null when leaving the activity, tried to reset it just before loading the new image, tried to force the garbage collector to run, nothing worked. I rechecked the logcat, and it appears that the Out Of Memory occurs on this line : `itemImage.setImageResource(resID);`. So, even if itemImage is set to null after every display, even if the activity showing it is completely terminated, it's not flushing the memory. It makes me doubt about all that I've learn these past 4 years. I'll try to follow your link, thanks for answers. – YumeYume Aug 09 '14 at 00:25

1 Answers1

0

Moving all of your drawables into a 'drawable-nodpi' folder within your res folder in your project may help. I know it freed up a lot of memory for my app that had a lot of large PNGs being displayed. Android background image memory usage

Community
  • 1
  • 1
Will Thomson
  • 875
  • 6
  • 19
  • They are already in this drawable-nodpi, but it allows me something like two or three more clicks, and still ends in out of memory.. Maybe I should try to use Bitmap instead of directly prints PNGs ? – YumeYume Aug 08 '14 at 18:54
  • You could also try adding this to your manifest android:largeHeap="true" It's not the proper way to address your memory issue, but if it gives you a few more clicks and still crashes Out Of Memory, then you should consider further scaling down your original images. – Will Thomson Aug 08 '14 at 20:11