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
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.
If I create a blank new project and run it, it uses 23MB of memory.
If I add one 540x960 png image to the background of the layout, it uses 47MB of memory.
If I add an image button with a background image on it, it uses 64MB of memory.
This continues to go on if I add things, and this is way too much. On the app I am currently working on, I have memory used at between 180-210MB of memory. I figured there was something wrong I was doing with loading the images, so I ended up putting them all on imgur, and loading them in with Glide. This reduced the memory by a small amount, and also doesn't explain why a new blank project would use 23MB.
I thought there might be some kind of setting, but I re-installed with default settings and am still getting the problem.
I experienced a similar issue when I put the drawable in the wrong resolution folder, especially the standard "drawable" folder is a pitfall since it is assumed to contain the image in "mdpi" resolution, so the image gets upscaled like crazy on xxhdpi devices.
Here's a simple test to figure it out, move the images into a folder called "drawable-nodpi" and check for the memory consumption.
Then make sure to provide the image in the right resolution in their dedicated folders, and avoid the "drawable" folder for images, just use it for xml drawables.
What basically happens is if you put your "540x960" image in to the standard drawable folder, it is assumed to be mdpi, if you view it on a relatively modern "xxhdpi" devices the image gets upscaled by a factor of 3 e.g it will be "1620x2880" and therefore consume much more memory.
http://developer.android.com/guide/practices/screens_support.html
I am using some png images for the backgrounds of activities in my application. These png files are mostly very small sized images. For example, I am using one with the size of 768x1024 which is actually 29.6KB on disk. When I run the application on my Samsung Note 1, I realized that the image actually consumes approx. 3MB of memory. So, Android seems to decompress the PNG file into a full ARGB bitmap (768x1024x4 bytes). I thought that this may be the result of the need of Android to resample/resize the image and placed the PNG file into the drawable-nodpi folder but this resulted in the same amount of memory consumption.
So, what should I do in this case, can I prevent this decompression behavior somehow? I have to put many different images for numerous activities in this project, so it seems that at a point this will cause out of memory errors. I am a beginner in Android and I don't know exactly how the system handles image resources, so I may miss something here.
Try adding these 2 lines to the in to the application tag in the manifest
android:hardwareAccelerated="false"
android:largeHeap="true"
But these are not recommended if you are building a memory efficient App,
But this really works.
I'm having severe memory issues in my application [1]. In order to investigate this, I took heapdumps of my app at different states. I saw that some bitmaps were taking huge amounts of memory. I wrote a small tool [2] that decodes the byte arrays to Windows bitmap files (.bmp), so that I can see the bitmaps and compare them to the files I have in my res/drawable folder.
What I discovered is that all my files are upsampled twice.
I first checked with the biggest one had: a byte array buffer of more than 9MB in the heap, which was decoded to be a nice 1920x1280 picture, while the original one was a 960x640 png file.
I tried with the second biggest, over 3MB, which once decoded showed a nice 754x1200 picture, the original size was... guess what? A nice 377x600 jpg file.
What gives?
I have enabled HW acceleration in my Android Manifest file (though I'm not sure I really need it, I'm just using some basic views and activities).
I'm running stock Android 4.0.2 on a GSM Galaxy Nexus (yakju). I'm receiving feedback from my testers that the issue is present on their 4.0.3 Nexus S, though I couldn't check their heap dumps yet.
I'm trying to save memory here, if Android doubles everything, no wonder the app crashes quickly because the heap usage gets too high (around 64MB in my case). I hope there's a reason and a way around it.
References:
OutOfMemoryError when loading activities
How to actually see a Bitmap taken from an Android heap dump
When you put images in res/drawable, Android will assume they have a dpi of 160, i.e. it is the same as putting them in res/drawable-mdpi. Galaxy Nexus is an xhdpi device, i.e. it has a (generalized) dpi of 320. To compensate for the high resolution display, Android will upsample the images with 200%.
The solution is simple, just put the images in res/drawable-xhdpi instead. Then the declared dpi of the images will match the display you run on, and Android will not perform any scaling of the images.
Please refer to http://developer.android.com/guide/practices/screens_support.html for more info.
I had an android app built especially for medium screen size android devices.
Now it was required to build that same app for a device called nook-color (by Barnes & Noble)
So for this we made all the images bigger & size & changed the layouts.
The result is that the apk which was 97.5 Mb earlier has now become 124 Mb in size.
Now the testers have complained that the application has become slower and it takes a little longer for any screen to open up. I am wondering whether this is due to bigger images or there is something else.
Can you please tell me what could be the reason for this.
124 Mb for an app seems to be a little bit excessive. I'd suggest trying to scale down the image assets, use Nine Patch images, as well as using PNGCrush
Yes. The delays are (at least in part) due to loading larger images (de-compressing them to bitmaps, to be more exact). Unless you can't help it, you should attempt to convert as many of these images to 9-patches : http://developer.android.com/guide/developing/tools/draw9patch.html.