I am loading images from database in may android app using bitmap.Since my app contains lot of images I am getting out of memory error. I have also given the permission android:largeHeap="true" in manifest file. I have also used system.gc() but nothing solve my purpose.The heap size gets increased as my app proceeds. How can I manage my heap size? I have also scaled the image when I am using bitmap.
You are testing your app on emulator or device??? Try to test it on device. Another thing is use lazy loading & same time you can use clearing cache to decrease memory load.
Related
Why does using large images using cardview crash the android system? I used 1280 X 600 images for cardview, but launching the app after install crashes the system presents a out of memory error from the logcat.
I had atleast 400MB of RAM while launching the app(viewed using simple system monitor), but yet the app crashes. Is the cardview designed to work with only small images? What if we have to use larger images for larger dpi devices? The cardview was used with a recycler view and the OS which it was tested on was KitKat.
You may have 400 MB RAM but all the RAM will not be directly provided to your application so application will crash...if you want to use different dpi images for different devices then you can add that to drawable, drawable-mdpi, drawable-xhdpi,drawable-xxhdpi etc. folder so that system can select them automatically....if you are loading from a url...then you can use third party libraries like Picasso, Glide etc to load the image efficiently
Also, in your question you are saying that you are adding card view in recycler view...which means more than one 1280x600 images will be loaded which will take a lot of memory and make your app to crash.
For your question "Is the cardview designed to work with only small images?"
No, cardview is not related to your out of memory... its the image size and Memory provided for you...card view can work with any size of image.
Adding Adding android:largeHeap="true" to your menifest can help you to some extent
I was running into the same issue. Adding android:largeHeap="true" to tag in AndroidManifest.xml solved the issue for me on my physical Android but I'm a bit concerned that this might still be an issue for some devices with less memory?!?
You don't mention how you want to show the image is it card background or a imageview into a cardview. If you want to use ImageView into a CardView then you can use picasso or glide library to handle this issue.
Out of memory error is very common error when you are developing for a application that deals with multiple images sets or large bitmaps or some Animation stuff. In this case we have to be very careful and efficient while handling the images or object allocation and deallocation. OOM error comes when the allocation crosses the heap limit or your process demand a amount of memory that crosses the heap limit.
I am trying to load an activity with a lot of pictures that serve as logo and buttons.
Apparently it crashes because there is not enough memory to support them.
What is the best or most efficient way to do sucha thing, as load the pictures I want without problems ?
I had look like this problem when use Genymotion emulator.so when i switch to default emulator of android studio this problem fixed although android studio emulator had less than memory than Genymotion emulator.
if your algorithms haven't problem but your application crashed you can use some technick
look like cache.
also if your image size is large you can reduce image size but you must have attention this work can reduce image resolution
Hi my Android application is working fine on my Lenovo a319 device , in this device it consuming 40 MB of RAM meanwhile on the Galaxy s5 Device , it consuming 300 Mb for the same build(apk). And I am getting lot of errors such as memory issue on Galaxy s5 device.
For Instance
Out of memory
exception and
android.view.InflateException: Binary XML file line
errors. I have found this Question where it is answered that high pixel resolution will cause memory exception , I am not able to get any clue from that answers provided in that Question , kindly provide the support on the same?
EDIT:
Thanks for the answers but they are inacurate i reuse recycle bitmaps and i make them null, even call system.gc(); and my memory is constant, i just do not understand that why it takes 300 MB of ram at sumasung galxy s5 and just 40 on my lenovo. My lenovo is hdpi s5 is xxhdpi if i am sure.
Looks like your app was built using layouts defined in xml files. These layouts uses images like images for background or source images for ImageButton or ImageView etc. All your drawables are usually stored in drawable-DENSITY folders. So you need to lower resolution of all of your drawables to save some RAM. Like if you have some drawable for ImageView lets call it new_image.jpg and its in drawable-hdpi and its resolution is 800x600 px try to resize it to 400x300 px (you could use a Farstone Image Viewer for this). Do that with every drawable in project and see what happens.
Also it could be that you are displaying images in ListViews using some ImageLoader like Glide (recommended) or UIL or Picasso or whatever use use. Such loaders are usually consuming a lot of RAM (which is configurable however) for caching needs and due to that at one point AOS fails to load, build and represent some your layout which uses images by itself.
Also I recommend you to lern how to display Bitmaps efficiently you MUST know such things.
This thing can be an easy fix for this
just add this line in your application tag in manifest file
android:largeHeap="true"
like this
<application
android:largeHeap="true"
android:allowBackup="true"
......
and if you want to more optimize your code then use Memory Analyser in eclipse
Apart from bitmaps and drawables the most common problem is leaking memory through the Activity context. Make sure that you are not passing around the activity to callback listeners.
A good tool to analyze memory is MAT(Memory Analyzer Tool). It helps you find out memory leaks in your app
I am Working with one android tablet application in which lots of work with Images.
i have to load hundred of large bitmap images in scroll view. one image is around 1Mb to 3 Mb.
i have already scale images using bitmap factory and sample size, and also load all the images in Async task.
i am loading all the images from SD card.
Still facing OUT OF MEMORY issue after scrolling some images.
any help would be appreciated.
thanks in advance.
hi hope your solution.
you can user Univarsal Image Loader. This is better api for image download and store in catch. and get from catch. it's a great api for image operation.
this case is often come while we getting bitmap so just use lazy loading. just take a look on this example
http://sunil-android.blogspot.in/2013/09/lazy-loading-image-download-from.html
To prevent your application from soaking up CPU time without getting anything done, the JVM throws this Error so that you have a chance of diagnosing the problem.
Its because of large bitmap memory in stored in native area
so it better i suggest you use libraries like Universal Image Loader or
Bitmap Fun from android
You have to find out when the OOM error occurs. Is it when you have too much bitmap cache in memory without releasing or you just meet it when you're decoding certain bitmap?
For the first situation, I think you should manage your bitmap cache yourself or using some effective 3rd-party library which mentions above.
For the second situation, I've met it on some low-performance mobile(usually with 512MB or less memory), when you decode a bitmap to requested size, it may needs a rather large memory to store the bitmap, for 240dpi devices, it's about 800 * 480 * 4 = 1.5MB, however you may fail to allocate that much memory on some machines cause they may have strict memory manage strategy to prevent that much memory allocation. You should just catch the OOM error and continue rather than get a crush.
Loading a bitmap in 1280x800 display will take 1280x800x4 byte (~4MB) memory. Java heap size is limited (~50MB) so you can not use lots of large bitmaps in your app. Using cache and reusing bitmap objects are necessary while working with bitmaps but another useful trick is to use larger hip size! to do so, add android:largeHeap="true" to Application tag in your manifest. It increases hip size to 4 or 5 times larger (~250MB) than normal. I hope this help other people reaching this thread while they search!
I am working on a wallpaper app. I have all my images stored in Drawable folder.
I am getting OutOfMemoryError when I add more than 30 wallpapers. I want to know how to overcome this.
Should I save all my images in SQLite Database and then load from there or should I have to do something else.
I have tried resizing bitmaps through Decode Bitmap Factory but it did not serve my purpose as the quality of images is reduced.
Resizing images at decode-time will only move your OutOfMemoryError ahead in time. That error means that your application leaked memory, or tried to use more memory than the available one. When working on bitmaps on Android, this happens quite often, because the limit is set around 25MB, and high resolution screens are increasingly common.
You have to redesign your application. There's no need to keep 30 images in memory, because they can't fit in a single screen - well, if they are thumbnail-size, you resize them all when you decode, and the total number of pixels in memory is the same as a single big picture, so you don't run out of memory.
You have to find a way to recycle() bitmaps when they are not visible. If you better describe your application, we can help you find the appropriate moment, also to preload images to have a responsive application and a better user experience.
I think what you need to do is display Thumbnails instead of drawable image on your screen. You can generate Thumbnails and display as per your size requirements. And whenever user click on Thumb, just take original path and set wallpaper.
Another option is you can use Universal Image Loader which helps you to buffer your image in disc (like SD card or your application's Internal memory). So issue of Out of Memory can be resolved.
If you want to control the crash of your Application then write your code in Exception block:
try {
...
}
catch(OutOfMemoryError error) {
//decide what to do when there is not more memory available
}
Also please the this link:
link