Android-Universal-Image-Loader icon indicating copy to clipboard operation
Android-Universal-Image-Loader copied to clipboard

Many errors OutOfMemoryError

Open moisesvs opened this issue 12 years ago • 14 comments

I'm having a lot of erros in my application of OFM. Specifically, when I am using images in viewpager because I'm generating a display option specified for each item of the view pager. Is this a memory problem?. I need depends on the content of one item while loading or other image is placed, so I generated a displayoption per item.

How I can avoid these OFM?. Thank you.

moisesvs avatar Jan 16 '14 11:01 moisesvs

if you're getting an out of memory error it is indeed a memory problem. avoid the errors by using less memory. look at the documentation to see how you can adjust the options to reduce memory footprint.

jbweimar avatar Jan 16 '14 12:01 jbweimar

Hi!

Before putting was not using caching images and not have the app OFM. But before giving the app OFM has 50% of the Heap memory available. Is this possible?

Thank you.

moisesvs avatar Jan 16 '14 13:01 moisesvs

when you say OFM you mean OOM?

On Thursday, January 16, 2014, moisesvs wrote:

Hi!

Before putting was not using caching images and not have the app OFM. But before giving the app OFM has 50% of the Heap memory available. Is this possible?

Thank you.

— Reply to this email directly or view it on GitHubhttps://github.com/nostra13/Android-Universal-Image-Loader/issues/513#issuecomment-32468222 .

jbweimar avatar Jan 16 '14 13:01 jbweimar

Yes, sorry,

This a mistake. The correct is OOM.

Thanks

moisesvs avatar Jan 16 '14 13:01 moisesvs

Hi, For more information, the reference to the library stacktrace is:

01-16 14:39:30.091: E/AndroidRuntime(23182): java.lang.OutOfMemoryError 01-16 14:39:30.091: E/AndroidRuntime(23182): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 01-16 14:39:30.091: E/AndroidRuntime(23182): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596) 01-16 14:39:30.091: E/AndroidRuntime(23182): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444) 01-16 14:39:30.091: E/AndroidRuntime(23182): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832) 01-16 14:39:30.091: E/AndroidRuntime(23182): at android.content.res.Resources.loadDrawable(Resources.java:2988) 01-16 14:39:30.091: E/AndroidRuntime(23182): at android.content.res.Resources.getDrawable(Resources.java:1558) 01-16 14:39:30.091: E/AndroidRuntime(23182): at com.nostra13.universalimageloader.core.DisplayImageOptions.getImageOnLoading(DisplayImageOptions.java:135) 01-16 14:39:30.091: E/AndroidRuntime(23182): at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.java:220) 01-16 14:39:30.091: E/AndroidRuntime(23182): at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.java:266)

moisesvs avatar Jan 16 '14 13:01 moisesvs

This is my DisplayImageOptions:

        displayImage = new DisplayImageOptions.Builder()
                        .showImageOnLoading(idResource)
                        .showImageForEmptyUri(idResource)
                        .showImageOnFail(idResource)
                        .extraForDownloader(imageResourceCard)
                        .resetViewBeforeLoading(true)
                        .cacheInMemory(imageResourceCard.isInMemory())
                        .cacheOnDisc(imageResourceCard.isInDisk())
                        .bitmapConfig(Bitmap.Config.RGB_565)
                        .imageScaleType(ImageScaleType.EXACTLY)
                        .resetViewBeforeLoading(true)
                        .build();

moisesvs avatar Jan 16 '14 16:01 moisesvs

hi Moisesvs:

have you been tried to set cacheMemorySize when configOptions of ImageLoaderOption? limit it with 2 MB and have it a try.

thanks

caiiiyua avatar Jan 16 '14 23:01 caiiiyua

Thanks I'm calculating device memory and works well if the cache memory to give it a half space. My question is: Is this enough space, is there a more logical way of doing this?

Thank you.

This is my code:

    // calculate avaible memory
    int availableMemory = (int) Runtime.getRuntime().maxMemory() / 1048576;

    // Create global configuration and initialize ImageLoader with this configuration
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
        .imageDownloader(new ImageDownloader(this, Constants.TIME_OUT_HTTP_CONNECTION, Constants.TIME_OUT_HTTP_CONNECTION))
        .memoryCache(new WeakMemoryCache())
        .memoryCacheSize((availableMemory / 2) * 1024 * 1024)
        .threadPoolSize(1)
        .discCacheFileNameGenerator(new CustomFileNameGenerator())
        .build();

moisesvs avatar Jan 17 '14 10:01 moisesvs

This error is played on a Samsung Galaxy Note 3. In a Nexus 5 reproduces less. Any help? Thank you.

moisesvs avatar Jan 17 '14 11:01 moisesvs

if you're getting OOM errors i wouldn't use a memory cache but a disk cache instead. i would also choose RGB_565 for the color setting unless you need alpha values in graphics. make sure you set the options so that the images are resized to the exact size of the imageview. it might slow stuff down a bit but it would save memory depending on what the dimensions of your images are in relation to the dimensions of your imageviews. a lso, don't use half the available memory.... it's a bit arbitrary. what if you only have 16mb? then you surely don't want to reserve 8mb for the cache.

On Fri, Jan 17, 2014 at 12:41 PM, moisesvs [email protected] wrote:

This error is played on a Samsung Galaxy Note 3. In a Nexus 5 reproduces less. Any help? Thank you.

— Reply to this email directly or view it on GitHubhttps://github.com/nostra13/Android-Universal-Image-Loader/issues/513#issuecomment-32599188 .

jbweimar avatar Jan 17 '14 11:01 jbweimar

Yes, down sample and resize to smaller images could save your available memory. Cache is something tradeoff, it is not always good to use more cache, on android, usually, you would cache just one or two screen of images is enough. for example, you have a listview with 5 items displayed within one page, you could only cache 5 to 10 of images according to your requirements. Calculate it as your reasonable cache size is enough:-) hope it helps

caiiiyua avatar Jan 19 '14 12:01 caiiiyua

Hi! What you say is a good idea. But for example in a ViewPager if I have an offset of 4 pages loaded. How do I get are maintained in memory only the two latest images and others go picking the disk cache?

Thank you.

moisesvs avatar Jan 19 '14 20:01 moisesvs

viewPager.setOffscreenPageLimit(0);

flamaniac avatar Jan 21 '14 14:01 flamaniac

It seems your images for loading stub (.showImageOnLoading(idResource)) is too big. Or you have memory leaks in your app.

nostra13 avatar Jan 26 '14 16:01 nostra13