OutOfMemory exception appears while scrolling the list of images

I have a list of 70 text items with image icons(which are stored in drawables folder). If I launch application the first time and scroll the list slowly - the exception doesn't occur.

When the application is launched the first time and I scroll the list with 'fling' action the following exception occurs:

java.lang.OutOfMemoryError: bitmap size exceeds VM budget
 at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
 at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:439)
 at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:322)
 at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:688)
 at android.content.res.Resources.loadDrawable(Resources.java:1710)
 at android.content.res.Resources.getDrawable(Resources.java:585)

After that, if I kill the application with DDMS, start it again and scroll it with 'fling' action, exception doesn't occur. So the exception arises only if application is installed and launched the first time.

Here is the adapter I use for the list:

public class AuthorAdapter extends ArrayAdapter<Author> {

private ArrayList<Author> items;
private Context context;
private LayoutInflater inflater;

public AuthorAdapter(Context context, int textViewResourceId, ArrayList<Author> items) {
    super(context, textViewResourceId, items);
    this.items = items;
    this.context = context;
    inflater = LayoutInflater.from(this.context);
}

static class ViewHolder {
    ImageView authorFace;
    TextView authorName;
    TextView authorWho;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.autor_data, null);

        holder = new ViewHolder();
        holder.authorFace = (ImageView) convertView.findViewById(R.id.authorFaceImageView);
        holder.authorName = (TextView) convertView.findViewById(R.id.authorNameTextView);
        holder.authorWho = (TextView) convertView.findViewById(R.id.authorWhoTextView);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    Author author = items.get(position);
    if (author != null) {
        if (holder.authorFace != null) {
            String faceFileName = author.getFace();
            String facePlaceName = context.getPackageName() + ":drawable/" + faceFileName.subSequence(0, faceFileName.lastIndexOf("."));
            int faceFileId = context.getResources().getIdentifier(facePlaceName, null, null);
            holder.authorFace.setImageResource(faceFileId);
        }
        if (holder.authorName != null) {
            holder.authorName.setText(author.getName());
        }
        if (holder.authorWho != null) {
            holder.authorWho.setText(author.getWho());
        }
    }
    return convertView;
}

}

Is there a way to avoid the exception? Should I catch the exception and escape the memory somehow?

Any advice how to accomplish this?

Thanks.


Asked by: Chester611 | Posted: 20-01-2022






Answer 1

You can't catch a java.lang.OutOfMemoryError. They're pretty much fatal.

Now, you say the problem only occurs the first time you run your application. This makes me think that the problem isn't with the view code. If a user flings a resource heavy view it can use a lot of memory, but as you say, normally your application works fine.

So - what initialisation do you do the first time you run your application? Is it possible one of the first-time run-once tasks is leaking memory meaning that there isn't enough left to use your view?

Answered by: Agata380 | Posted: 21-02-2022



Similar questions

OutOfMemory Exception while using FileBody for sending images to the server in Android?

I am using FileBody for sending images to the server from Android. But when sending bigger images the application crashes with OutOfMemory exception. Is there some way of overcoming this? Or has anyone faced the same problem with FileBody. Please help me out in this regard. Regards Sunil


memory - In my Android app, every other time I launch my app, it crashes with OutOfMemory Exception

I have an Android app that in the onCreate() method, preloads a lot of graphics. When I test my app on my HTC Aria and launch it, it runs fine. However, if I press the back button to exit my app, and then launch the app again, it crashes with an OutOfMemoryError: bitmap size exceeds VM budget. If I then launch the app for the third time (right after it has crashed) it launches fine. Then if I close and re-launch...


android - OutOfMemory exception when loading bitmap from external storage

In my application I load a couple of images from JPEG and PNG files. When I place all those files into assets directory and load it in this way, everything is ok: InputStream stream = getAssets().open(path); Bitmap bitmap = BitmapFactory.decodeStream(stream, null, null); stream.close(); return new BitmapDrawable(bitmap); But when I try to load the exact same images from sd card, I get an O...


Android: OutOfMemory exception when loading animation

I'm doing things pretty simply but I have a lot of frames. It seems to be the amount of frames that causes the exception. I've tried to reduce the size of each frame, but that doesn't seem to effect much. This is even before the animation starts. Short of showing less frames, what can be done? Is there another approach? Adapted from sample code: public class XMLAnimation extends Activity { c...


java - OutOfMemory Exception when handling images

This question already has answers here:


exception - ANDROID: Help, outofmemory bitmap exceeds budget size. how to recycle?

I've hit a wall with my Android-app, as I'm getting an OutOfMemoryException with the description that my "bitmap exceeds VM budget" and so on. The weird thing is that the code works on one PC but triggers this exception in another. I have like 10 900x500 images and the code I'm using is enclosed below. I have tried to recycle(), which renders both the errors and the images invisible. I re...


android - Json Object is giving OutOfMemory Exception

I'm using JSON Framework to update the data of client side from server. I have 30 Pojo Classes and i received the Http Response from server. I create the Object of Reader by using the method InputStream instream = entity.getContent(); reader = new InputStreamReader(instream, "UNICODE"); And i pass it in the Json Object like this synchronizationResponse = gson.fromJson(reade...


java - Why I am getting OutOfMemory Exception?

I am getting OutOfMemory Exception. Why? I am using this code for logging. Does this approach correct? Exceptions and closing of streams are handled in parent methods. private static void writeToFile(File file, FileWriter out, String message) throws IOException { if (file.exists() &amp;&amp; file.isFile()) { if ((file.length() + message.getBytes().length) &lt;= FILE_MAX_SIZE_B) { out...


outofmemory exception in android

I have two activities in my application Activity A and Activity B. From Activity A I am going to Activity B. What will happen to my application if the Activity B throws an OutOfMemory error? Will the application show Activity A?


OutOfMemory Exception on Android

I'm currently facing a problem where user upload a smaller size image, there will be no problem. However, when the user chooses to upload a bigger size image, the application will throw OutOfMemory exception. Should I set an image size limit that can be uploaded?


OutOfMemory Exception while using FileBody for sending images to the server in Android?

I am using FileBody for sending images to the server from Android. But when sending bigger images the application crashes with OutOfMemory exception. Is there some way of overcoming this? Or has anyone faced the same problem with FileBody. Please help me out in this regard. Regards Sunil


java - Android - A different Bitmap wallpaper for every home screen - OutOfMemory

I am trying to set a different wallpaper for every home screen, but I get OutOfMemory issues. I have 5 Bitmaps which I am trying to overlay on a wallpaper Bitmap which is 5 times the display width. Using the code below I get OOM. The problem seems to be the first line of code which creates the large wallpaper Bitmap. My question is whether there is a way to do this (i.e. some way that takes up less memory, or someway to al...


android - Bitmap OutOfMemory on Multiple Screen Changes

I have a bitmap that I load from the SD card by allowing the user to choose a picture to display. Once the bitmap is created, I set the Bitmap in an ImageView: mBitmap = Bitmap.createBitmap(Media.getBitmap(this.getContentResolver(), mPictureUri)); mImageView.setImageBitmap(mBitmap); This works fine. But, if I change the screen orientation from portrait to landscape, and back again a few ti...


java - Live Wallpaper OutOfMemory Error when i have more than 30 frames

I've been trying to make a simple live wallpaper out of a boot animation. So basically i have about 50 .pngs in my drawable folder. I'm able to set the animation to about 10-20 frames and it works great. But once i set it to about 30 frames...I get an OutOfMemory Error. I was hoping maybe someone could take a look at my code and maybe give an example of how I could achieve more frames? That would help so much i've been loo...


memory - In my Android app, every other time I launch my app, it crashes with OutOfMemory Exception

I have an Android app that in the onCreate() method, preloads a lot of graphics. When I test my app on my HTC Aria and launch it, it runs fine. However, if I press the back button to exit my app, and then launch the app again, it crashes with an OutOfMemoryError: bitmap size exceeds VM budget. If I then launch the app for the third time (right after it has crashed) it launches fine. Then if I close and re-launch...


OutOfMemory while editing XML for Android in Eclipse

I create Android applications for my job. Eclipse is open all day and I am frequently editing XML files for layouts, etc. I find that creating/editing these XML files pushes up the memory use of Eclipse. Eclipse never seems to release any of the memory and eventually Eclipse tells me that there has been some sort of workbench out of memory error and I have to exit. It will not let me save or do much of anything afte...


android - OutOfMemory exception when loading bitmap from external storage

In my application I load a couple of images from JPEG and PNG files. When I place all those files into assets directory and load it in this way, everything is ok: InputStream stream = getAssets().open(path); Bitmap bitmap = BitmapFactory.decodeStream(stream, null, null); stream.close(); return new BitmapDrawable(bitmap); But when I try to load the exact same images from sd card, I get an O...


memory management - OutOfMemory in Android

In the solution for out of memory: Strange out of memory issue while loading an image to a Bitmap object, it talks about using: decodeFile(File f). How do you handle internal image files that are referred to as R.id.something? these resources are seen as integers?


OutofMemory error in Android

I have created one game which uses images and my game is in portrait and landscape mode. I have 2 different layout for that When i switch from 1 orientation to other, after doing this 5 6 times, it gives force close error Error is like : Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget Anyone know the solution? is there any way to clean up all images in switching orientation ? thanks in...


how to avoid outofmemory error android

Hai Friends, i am trying to load 20 images.I get outofmemory error. how to avoid outof memory error .plz help me;






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top