thest1 / LazyList

Lazy load of images in Android
http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview/3068012#3068012
MIT License
1.2k stars 497 forks source link

Out of Memory Error : Recycle? #20

Open shaunidiot opened 12 years ago

shaunidiot commented 12 years ago

Please forgive me for my noob-ness in Android programming. Just learnt Java weeks ago.

I've been using Lazylist to load images from website. However, I've been getting OOM error. I searched it up on Stackoverflow and realized that recycle is needed so that space can be allocated for more images. I looked it up on ImageLoader and this (recycle) wasn't implemented. I tried to code it myself yet faced with lots of error [bitmap.recycle()]. May I ask if this is possible? Furthermore, in the listview, one image doesn't cause an error, yet when I add textview etc, it caused the OOM error too. Thanks!

thest1 commented 12 years ago

Sorry for a delay, too busy these days. If you get OOM probably you do something wrong. Please check what's the difference between your implementation and my sample. Actually you should not call recycle because bitmaps are cache in memory. If you don't cache them the scroll will be too slow. They're cache in memory for some time and removed from memory when we don't need them. I use this approach in many applications using a lot of bitmaps, work just fine. OOM issues are very rare for me.

2012/10/4 shaunidiot notifications@github.com

Please forgive me for my noob-ness in Android programming. Just learnt Java weeks ago.

I've been using Lazylist to load images from website. However, I've been getting OOM error. I searched it up on Stackoverflow and realized that recycle is needed so that space can be allocated for more images. I looked it up on ImageLoader and this (recycle) wasn't implemented. I tried to code it myself yet faced with lots of error [bitmap.recycle()]. May I ask if this is possible? Furthermore, in the listview, one image doesn't cause an error, yet when I add textview etc, it caused the OOM error too. Thanks!

— Reply to this email directly or view it on GitHubhttps://github.com/thest1/LazyList/issues/20.

DeLL116 commented 11 years ago

I was having the same issues, specifically on the Motorola DroidX2 which has a 32mb heap limit AND is running Gingerbread......however....

I changed the Map instance of the cache object in the MemoryClass.java file to use a SoftReference to bitmaps like so:

private Map<String, SoftReference<Bitmap>> cache=Collections.synchronizedMap(
        new LinkedHashMap<String, SoftReference<Bitmap>>(10,1.5f,true));

Now I'm able to load up over 300 images all of great quality.

Obviously all calls to get() and put() needed to be modified to account for the change to SoftReference, but it's working great now.

I'm a noob, so if I'm dead wrong on this my apologies, but I thought I should let you know of my findings just in case it could help others.

Thank you for this library! It's awesome!

thest1 commented 11 years ago

SoftReferences are not recommended to be used on Android. They may be cleared too fast and you'll get no cache at all, image will be parsed from SD each time. I had this problem before that's why I switched to my own MemoryCache implementation.

By default MemoryCache uses 25% of available memory. You can try to use less to have more free memory.

asiri-07 commented 11 years ago

Dear thest1, first, thanks for the awesome library. However, I am facing the same issues as DeLL116 and the first guy. Could you pl ease re-check your approach for memory leakages with some heavy loading ?

ankit662003 commented 10 years ago

hi @thest1 this is my app,it's work in progress and i am unsure if this is the way lazyload performs please have a look at the app,it's only 1 mb https://www.dropbox.com/s/anijtvbs4gp012t/PlayMee.apk

you will really like the app and i will mention your name on my company's website i am 20 yrs old and I am a student ,so please help me out,I am not having errors but I think lazy load isn't performing to its potential...i tried saving memory by using weak references/soft references until i came across your answers,please help me out with this one. I have used view holder pattern to increase the performance by not intializing the views again and again,it did help me to some extent but it did't solve the laggy scroll issue I know you are busy and all but I think I really may need your help on this one as I am just 20yrs old and may need some guidance. If you have decided to help me here is the code you may need to look

https://www.dropbox.com/s/t7uoudwq30i58kx/LazyList.rar

I have a website under construction www.playmee.org and i will mention your name there,if you would like please help me out on this one,thanx

thest1 commented 10 years ago

Laggy scroll usually means you're inflating new view in every getView() call or decode bitmap in every getVIew() call. Please investigate what exactly is happening when you scroll.

vnju commented 10 years ago

Dear thest1 You're a cool man. Your project's really helpful for everyone. However, I also met a memory issue that
I used youtube user feed Project at https://github.com/coolbong/android-youtubeplayer, when i ran, it only shows around 30 images on listview. When i did ClearCache and pull to refresh, It can show more, but above images were disappeared Could plz help me Thanks alot

vnju commented 10 years ago

I used SoftReference and everything's ok now. However, I want to get the solution from Thest1 thanks

d7coders commented 9 years ago

I have the same issue, any solution ?