Android RuntimeException vs. OutOfMemoryError - android

I have developed a PhotoViewer application wihch simply lists the image files in external storages and shows them if the user wants to see one of them. My problem occurs when my thread drawing the thumbnails for the images.
If the count of images is less than about 500 it is ok for me and there is no issue but if the user has over 1000 images (it is quite possible) afters some drawing thumbs the memory usage exceeds the limit and I get OutOfMemory Exception.
I have tried to use bitmap.recycle() but this time I have faced with using a recycled bitmap error (RuntimeException). Could you suggest me a way to show more and more thumbnails (over 2000 maybe) for the images in external storages?

Your OutOfMemory problem will probably be resolved if you got recycle() working correctly. The RunTimeException will be an ImageView still trying to draw a bitmap which has been recycle, so you must ensure that nothing is using the bitmap before you recycle it.
You could also stop thumbnails being loaded whilst the ListView is being scrolled using a ListView.OnScrollListener - without this, if the user flings through the list, all intermediate rows that scroll past are still getting loaded.

Related

Out of memory exception on inflating layouts

I am getting random out of memory exceptions in my app caused by inflate exceptions.
I have 7 fragment (difficulty) activities which launch their own activities via buttons. Each fragment has a scroll view with 30 buttons (levels).
I have set it up so that i can swipe across to each fragment and the fragment takes up the entire screen.
Occasionally when i swipe a few times and then select a random button from a group of 30 it will crash. It tries to load the activity and gives an out of memory exception with an inflate exception on a random line. The line always falls on an imageView or imageButton in the xml file. The activities that load are a grid of imageViews and imageButtons.
I do not get the exception much but it is something i want to fix. I have looked at many other out of memory exception questions although none have helped me. I have done a Memory analyser test and it shows nothing out of the ordinary.
I believe that the imageViews and imageButtons are using too much memory, although i only ever have one activity open at once.
It IS because of your images that are loading. When you load an image and you move around the page and view another image the heap increases. As you continue the process of viewing random images the heap grows even more until your app crashes. It's like stacking books on a glass table. You either move(cache) a book(image) or the glass(app) breaks. You should use an imageloader to load your images.
https://github.com/nostra13/Android-Universal-Image-Loader
You've mentioned that it always falls on an ImageView and ImageButton - and this is the clue to solve this problem. You get OOM 'cause background resource of this view has high resolution and takes a lot of memory. Try to lower resolution of this image.
Also you've mentioned that you have a ScrollView and this means that you keep in memory every 30 items. Probably you'd better change it to RecyclerView backed by adapter.
Just had the same problem and I'd like to simplify all the things said here:
Simply: reduce your images sizes.
Don't use 1080X1920 images... It's too high res.
Such image, even if compressed, when deployed will catch about 1080X1920X4B = 8.2MB (The GPU has to deploy it to it's full original resolution... That's why compression won't help but reducing the needed memory size...) and this i RAM that we're talking about..
Take Gimp or Photoshop and down scale the image to, say, 1/4: 540X960, and you won't feel the difference.. Belive me, been there already.
Beware of the memory consumption of images and videos.
Hope this helps,
James

Managing Memory for showing large bitmaps in android

I know this question is asked several times, but still clarity to my situation would be helpful.
I am showing some images in 2 column grid view. When user taps on an image, I am displaying the image in ViewPager. The Image which is displayed in gridview is about 200X200px and I want to show the same image with enlarged size say about 800X800px in ViewPager in a DialogFragment.
The Actual size of images are huge with different resolutions. I have followed the link http://developer.android.com/training/displaying-bitmaps/index.html and I have scaled down images to size which is required for my app. In ViewPager fragment dialog I am also recycling the image with bitmap.recycle() and calling System.gc() explicitly(I know this is a bad practice) at PagerFragment onDestroy. But even then I am getting Out of memory error. On top of it, I am encountering this issue only in Android 4.2.1 (Nexus 7) but not on Android 4.1.2( Samsung Tab) and Android 4.4(Nexus 7).
For later 2 android versions, I do not have to call even System.gc(). It works very well without this. I have checked Viewpager with some 300 - 400 images. But for former after scrolling 60 images, App is crashing with OutOfmemory error. To resolve this I have used a workaround for now android:largeHeap="true" which I think is very bad for myapp and could not digest it.
I really appreciate if anyone who can help me avoid android:largeHeap="true".
In my app, I have to show around 6000 images in GridView and also an enlarged image in ViewPager linked to GridView. I am loading only 20 images from FileSystem asynchronously while scrolling gridview.
Thanks in advance...
Use an LRUCache to hold the images. Make that the only thing that holds a Bitmap that's not currently on screen. That way the older bitmaps will be kicked out and garbage collected quickly.

how to clear memory when it is full because of using too many backgrounds in android

Hi guys i am new to android and i posted a question a week ago in this link which basically stated that i was getting a java.lang.outofmemory error when i was using a lot of different backgrounds for my activities.
why am I getting errors when I use different backgrounds in xml
So as a new developer I have searched and searched for a solution as to how to clear the memory as i go from activity but none have been clear or precise. Then i stumbled across this site http://androidactivity.wordpress.com/2011/09/24/solution-for-outofmemoryerror-bitmap-size-exceeds-vm-budget/
which described exactly what i was going through except they use 10 activities and i am only using 4. However when i implemented his code it my project i ended up with null pointer exceptions and after fiddling with his code I ended up back were i started with the same out of memory error.
So can anybody direct me to someone who can show me how to have as many backgrounds as i want with out running out of memory. Or does android as great as it is does not let you simply use more than a certain amount of backgrounds? help?
It's not that there is a limit on the amount of backgrounds, but each background image you load is a loaded into memory as a bitmap and held there until the activity is destroyed. If you are opening multiple activities one after another, each background image will need to be held in memory and so eventually you will get an out of memory exception.
If you set a large background image, you will also experience some blocking on the ui thread, while the image is loaded into memory.
One way around this that worked for me was to use an imageloader. This decodes the image off the ui thread, caches it on disk, loads it into memory and if memory is running low, will clear an image from memory and fallback to the disk cache. You may get a slight delay/fade in as the image is loaded but this is not so bad visually, and when loaded once, will load straight away if you go back to that activity.
Check out Picaso Picasso which is really easy to implement and a great api or Universal Image Loader.
My layouts were all RelativeLayouts and the first child (will be behind all other views) was an ImageView with scaleType centercrop and width and height set to match_parent. When each activity loads (onCreate), just grab a reference to the imageview in your layout and set the required background image using your ImageLoader of choice.
The other option is to have multiple copies of your background image in your resources, with each one resized to perfectly fit your resolutions of choice (drawable-mdpi/-hdpi/-xhdpi etc). This way, you ensure you are never loading images that are way bigger than you need to be displayed and your app will be more forgiving in terms of memory consumption.

How to recycle bitmaps shown in Gridview

I am using GridView in my app. The images for gridview are
I download them from network.
Downscale them to required size.
Write them to file,
All 3 steps are done using AsyncTask. Then I update UI. Next time when I open GridView, I simply decode it from sdcard. The image count is around 15. When I do memeinfo dumpsys on my app, my heap size keeps growing from 12 to 50. My question is , will gridview automatically recycle the bitmaps passed in adapter or we have to do for it. I get OutOfMemoryException.I see many links how to handle bitmaps efficiently including android link and I have implemented them in my app but no link explains bitmap recycle in gridview.Please help me.

Android gallery with bigs images for zoom

I try to implement a Gallery where each item is a big image for a good zomm-in function.
The problem is the management of the memory.
When i try to go to the fourth element android goes to Out Of Memory, so i try to recycle the images not displayed, but android goes to RunTimeException for try to use a recycled image when i come back.
Also i try to change the bitmap of the bigs images not displayed with bitmap of small images but always goes to Out Of Memory.
How can i do, for manage the memory of the Gallery?
After you do call recycle(), make sure you don't use that Bitmap reference again as that will cause a runtime exception if certain methods are invoked. Try using SoftReferences wrapped around your bitmaps. This will guarantee that all of the memory that is no longer references will be reclaimed before an OutOfMemoryError is thrown.

Categories

Resources