How does Android memory allocation work with ImageView?

I'm loading approximately 20 pictures of size 50-70K from server, then display them in a ListView. Initially I stored the data as Bitmap which causes memory running out quickly. Then I decided to compress all these bitmap files and store in Content provider Media. So in my adapter, the user data only contains the Uri to the image file.

However it didn't fix the problem at all, it run a bit longer, but still crashed after loading about 10 pictures or so. Here is the error log from the compiler.
1048576-byte external allocation too large for this process
VM won't let us allocate 1048576 bytes

I even clean up each bitmap data after setting it to my ImageView, plus delete the all the image files which are stored in my sdcard

@Override public void onDestroy() { // clean up for (User user : userList) { getContentResolver().delete(user.getImageUri(), null, null); } super.onDestroy(); } private Uri constructUriFromBitmap(Bitmap bitmap) { ContentValues values = new ContentValues(1); values.put(Media.MIME_TYPE, "image/jpeg"); Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values); try { OutputStream outStream = getContentResolver().openOutputStream(uri); bitmap.compress(Bitmap.CompressFormat.JPEG, 50, outStream); outStream.close(); } catch (Exception e) { Log.e(TAG, "exception while writing image", e); } bitmap.recycle(); return uri; }

Now I ran out of idea, I really don't know what could go wrong in this case. I wonder if anyone has experienced this issue could shed me some lights?

Since the code is quite long, I only extract the main functions: Here is my User class data:

public class FriendFeed { // required parameters private final int activityId; // in case we want to handle the detail of // this activity private final int friendId; private final String friendName; private final Challenge.Type challengeType; private final String activityTime; private final String placeName; // optional parameter private String challengeName; private String challengeDescription; private Uri activitySnapPictureUri = null; private Uri friendPictureUri = null; private String activityComment;

And here is my main function:

@Override protected Boolean doInBackground(Void...voids) { JSONArray array = JsonHelper.getJsonArrayFromUrlWithData(GET_FRIEND_FEED_URL, datas); if (array != null) { try { for (int i = 0; i < array.length(); ++i) { Uri snapPictureUri = null; Uri userPictureUri = null; if (Challenge.returnType(array.getJSONObject(i).getString("challenges_tbl_type")) == Challenge.Type.SNAP_PICTURE) { snapPictureUri = constructUriFromBitmap(ImageHelper.downloadImage(array.getJSONObject(i).getString("activity_tbl_snap_picture_url"))); } if(ImageHelper.downloadImage(array.getJSONObject(i).getString("users_tbl_user_image_url")) != null) { userPictureUri = constructUriFromBitmap(ImageHelper.downloadImage(array.getJSONObject(i).getString("users_tbl_user_image_url"))); } publishProgress( new FriendFeed.Builder( // required parameters array.getJSONObject(i).getInt("activity_tbl_id"), array.getJSONObject(i).getInt("friends_tbl_friend_id"), array.getJSONObject(i).getString("users_tbl_username"), Challenge.returnType(array.getJSONObject(i).getString("challenges_tbl_type")), array.getJSONObject(i).getString("activity_tbl_created"), array.getJSONObject(i).getString("spots_tbl_name")) // optional parameters .challengeName(array.getJSONObject(i).getString("challenges_tbl_name")) .challengeDescription(array.getJSONObject(i).getString("challenges_tbl_description")) .activitySnapPictureUri(snapPictureUri) .friendPictureUri(userPictureUri) .activityComment(array.getJSONObject(i).getString("activity_tbl_comment")) .build()); } } catch (JSONException e) { Log.e(TAG + "GetFriendFeedTask.doInBackGround(Void ...voids) : ", "JSON error parsing data" + e.toString()); } return true; } else { return false; } }

-------------Problems Reply------------

Android enforces a per-process memory allocation limit of 24MB so you can't allocate more than that. However, 20 pics of 70K each should amount to 1.4MB only... so my guesses:

  1. Maybe you're allocating Bitmaps in other parts of your app, so that there's less than 1.4MB available for your bitmaps on this ListView.
  2. Memory leak somewhere
  3. If you determine that you really need all the bitmaps you're using, are you sure you need the bitmaps to be this large or have this much resolution? Reducing them can help.

If all else fails and you do need lots of bitmaps in memory, you can always use OpenGL textures.

Category:android Views:0 Time:2012-01-06

Related post

  • Android - memory allocation while opening new activity 2011-08-25

    let's have activity A and button in that activity, which opens activity A again. Stack looks like after couple clicks like: A - A - A - A - ... - A My questins go towards memory allocation. When I start x - times activity A, would it be allocated in

  • Android - Memory Allocation Tracker not tracking my test allocation 2011-01-05

    I'm currently playing around with the memory allocation tracker of the DDMS tool. In my project i insert the following line in a button's onClickListener: memTrackerTest = new byte[1024*1024]; memTrackerTest is a private variable of the Activity. I w

  • Android memory allocation 2010-01-25

    I am getting a "bitmap size exceeds VM budget" error. I have read that there is a 16MB memory limit. In this thread Romain Guy says that "you can only allocate 16 MB of memory for your entire application". However, my app must be running out of memor

  • Android Memory Allocator - Does memory need to be contiguous? 2011-01-28

    Does free heap space have to be contiguous for gralloc() calls to succeed? Or can gralloc() return sparse free heap segments to satisfy a request? i.e. heap fragmentation --------------Solutions------------- I'd say that's up to the UMP device driver

  • Android: reading accelerometer without memory allocation? 2011-02-12

    I am developing a game for Android (2.1+), using the accelerometer as user input. I use a sensor listener that I register at the beginning of the activity with the sensor manager, as follows: mSensorManager.registerListener(SystemRegistry.inputSystem

  • memory allocation when using CCSprite array in cocos2d-android-1 2011-06-08

    I am using cocos2d-android-1 for my gamedevelopment. I have some confusion related to memory allocation. Suppose I need an array of sprites. I will declare them as CCSprite mySprites = new CCSprite[MAX_SPRITES]; and while creating i will create them

  • ow to get memory allocation by content(ex. video, music) in android? 2012-01-19

    I am developing small application, in that i have to collect amount of memory allocated for video files and audio files in both onboard and external storage on android device. Can we take memory allocation by content (e.g. video, music) in android? i

  • Android - get USS memory allocation programatically 2011-08-26

    I force this problem: I need to know USS memory allocation (= how much RAM is allocated by your APP) for my app. I know, it is possible to get this info from "adb shell procrank", but I want to have it programatically. Is there any way how to do it?

  • Android Memory Leak solution 2011-04-28

    I'm having problems with memory leaks. When I show an Activity every thing works. When I press back and try to reload the Activity I get an outOfMemoryException. So from the docs I've read I can conclude that in this activity not all references are d

  • How much is too much memory allocation in NDK? 2010-06-14

    The NDK download page notes that, "Typical good candidates for the NDK are self-contained, CPU-intensive operations that don't allocate much memory, such as signal processing, physics simulation, and so on." I came from a C background and was excited

  • Dalvik Memory Allocation - How to change the default limits? 2010-06-20

    I am currently considering to develop an application for processing of images captured by the camera of mobile phones using the Android OS. Before I make the decision to go forth, I have done some research and found out there are some complaints rega

  • Android ==> Memory Analysing ==> Eclipse memory analyzer? 2010-06-24

    I need to check my application for memory leaks, i also need to see the memory allocation of my application. I downloaded and installed eclipse memory analyzer, and it looks like the first step is to open a heap dump. But what is a heap dump, how can

  • Android Memory Usage Problem on possibly using ActivityGroup 2011-02-25

    Android Memory Usage Problem on possibly using ActivityGroup This is a little bit long story that i ended up messing with memory problems. I developed very deep android application by using very amount of Activities and Frameworks. First of all I hav

  • Tutorials or examples for Android memory debugging 2011-02-26

    So, I'm blowing up the heap somewhere in my logic thread of the Android game I'm making, it plays fine until the heap randomly grows from ~3MB to almost 30MB, whereas the game hangs (quite understandably). I've been hacking away at this bug for a few

  • How to get external memory allocation in Honeycomb? 2011-03-14

    In pre-Honeycomb versions of Android I tracked external allocation of memory VMRuntime.getRuntime().getExternalBytesAllocated() or with Debug.*ExternalAllocSize(). Now VMRuntime is not available in Honeycomb and all Debug.*ExternalAllocSize() methods

  • OpenGL Memory allocation problems for drawing 2011-05-23

    I'm very new to Android and I'm writing my first app which consists of drawing STL data (a bunch of triangles). For a small number of triangles (~ 4000), my app works great. But as soon as I try to load large data (~ 100000 triangles), I get memory a

  • Minimizing memory allocations with handlers, messages, and bundles 2011-05-27

    I have an activity that constantly (several times per second) sends byte[] data to a service via AIDL. The service forwards these messages using a message-bundle* to a network thread's handler. The network thread also constantly receives data, which

  • Android memory management question 2011-09-17

    I have bunch of queries regarding android memory management . I know each app is provided with jvm , but how about memory size , do they increase and decrease with respect to other apps on phone? The main part how do jni memory allocation in each jvm

  • How to analyze the change of native memory allocation 2011-11-25

    I use Android DDMS's native heap tool to analyze the memory allocation of native code. This tool is very useful. However, I'd like know newly created native memory when I do something on my phone. It seems that DDMS does not have this functionality.

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.174 (s). 11 q(s)