My relatively small application for Android with nice graphics that consists mostly of 9patch drawables consumes about 10MB of the memory.
Do you think that it is OK? Or I should optimize it somehow? What is acceptable memory consumption for small applications?
To get approximate per-application memory limit for any device you can use Activity member function
public int getMemoryClass ()
There is a 16/24MB memory limit for application running in android. This thread gives you more info..
This tutorial talks about some good memory management practices..
There is no rule that small app should not take more than x mb memory. The default value of memory allocated by the Dalvik VM for each application is of 16 MB, using the Android 1.6 OS and higher. As long as your app is using memory under this limit, its perfectly fine.
If you really want to optmize your code, always make sure, you don't have any memory leaks in your app, and you are clearing your resources after use. That would only be the trick. :)
10MB has been fine in my experience. The smallest Max Heap Size you are likely to come across is 16MB, and a lot of devices have higher than that now.
I'm not sure why the drawables are taking up so much ram though. Maybe try using a zipaligned APK if you are not already. The export signed APK wizard in eclipse is an easy way of getting one - the development builds are not generally zipaligned.
Related
I recently created an android application, and after I completely redid my spinner dropdown menus using a custom adapter, some devices are crashing. It's very minimal, like 3 people have experienced crashes ever, but still it's something I want to address if possible because the apps minimum SDK version is as low as 16, so I imagine it's older devices that struggle the most. Attached is a pic of the crash report! This specific device has 2 GB of memory, you would think that would easily be enough to load a spinner with a bunch of low quality images right?
Thanks for the help in advance! This is the crash report: https://i.imgur.com/Wtm5pX9.jpg
The amount of memory the device has != the amount of memory you can use. The OS takes a lot. Other apps take a good amount. And even the memory you can use is fragmented into different pools. Generally "OutOfMemoryException" means out of Java heap memory (out of native memory would be a different crash, for example). Bitmap memory goes into different buckets on different OS versions, they've changed it a few times. So it could be a variety of reasons- you have too large an image that's using insane memory, your have leaks, you have sufficient memory on the device but the heap allocation spiked for some reason, your network layer isn't efficient (if you use Volley to download images its particularly stupid about that). There's not enough info here to actually give you a suggestion. I'd try to replicate it on a simulator with the RAM purposely capped at 1GB or smaller and see if you can reproduce it.
As I researched, Android allocates limit memory for each process, maybe range from 16MB to 24MB for each one. Here is reference
Nevertheless when I view memory usage for one application in setting, I often see one normal application costs hundred megabytes for memory (on one process). There is a conflict here that I cannot understand.
Thanks :)
NDK code can use more system RAM than can a single Dalvik/ART process. Also, the app might be using more than one process, or it might be using android:largeHeap to request an above-normal heap size.
I have developed HelloWorld Android Application which just prints Hello World using eclipse and from tutorial https://developer.android.com/training/basics/firstapp/index.html?hl=it,but to my surprise the app takes 2.21 MB of memory. Can you please suggest way to reduce this size to few kb's as this should not take much space, as I haven't added any images or code in it.
An APK is a zip file, you can open it and figure out what's taking the space. If that doesn't hint you enough come back and add this info.
Also, take a look a look at proguard
Edit: Oh wait, I might have misunderstood you, did you mean storage space or runtime memory ? (RAM)
Are you sure you are talking about runtime memory? Even a simple "Hello world" application with no icon from the default Android project has a 10MB heap with 9MB allocated on my phone. Its installed size is listed as 1MB.
I do not know how to reduce memory usage in such a simple app but I can give you some tips to reduce installed size; however, there is a limit to how small you can go.
If you have an icon for all screen resolutions from mdpi to xxhdpi, it will cost you 44KB. I have found the practical lower limit for a usable app to be a little above that; I have a reasonably small app that is only 95KB. However, this is expanded during installation; expect your app to take up to twice the APK's size once installed.
A good way to get rid of space for a small app is to remove the support library. It is included by default in new projects, and takes from 400-600KB. However, removing it comes at a cost - many user interface improvements such as fragments are only supported on older platforms using the support library, so you will either have to restrict the tools you are able to use or your target user base.
Only way i found to reduce runtime memory is to use various optimization techniques. Here is the tutorial Android Dveloper. This tutorial will help you in increasing performance of your application as well as reducing the runtime memory consumption of your application.
I'm developing an android app that has a lot of bitmaps that uses a lot of RAM. My question is that when I keep my app running for a long time and the ram usage exceeds 64 MB, the app crashes with an "OUT OF MEMORY ERROR, VM won't let us allocate ... etc".
While other apps such as facebook reaches 200 MB ram sometimes without any crashes and with a very fast performance. My device is Galaxy S II.
and please notice that my question is not about reducing the memory usage, it's about the difference in memory limit between my app and other apps.
Thanks.
I think there is a limit on the amount of memory that an application can use... Used to be 32MB... There might be a permission that you can change to request more memory, in the same way that you can request hardware acceleration. From a quick google android:largeHeap="true" might be your answer... Also Runtime.getRuntime().maxMemory(); might help? This link was also interesting...
The amount of memory that the vm is reserving for each application is not part of the Android ecosystem and can't be determined, it's part of the configuration given by the manufacturer.
Usually the last word about the memory size for each vm is given by the file build.prop.
Do not use the NDK trick if you do not know what are you doing.
I have an android app that uses alot of memory doing pixel manipulation. And what I have noticed is that android does not kill programs or free memory in favor of the foreground app. And my app just crashes with not enough memory errors. Right now I have it autodetect how much memory is left and scale pictures appropriately. This prevents crashes but results in poor image quality.
Is there a way to tell Android OS, free up memory as my app is memory hungry. From what I read from android, the OS should do this automatically. But it doesn't appear to do it. Maybe I'm missing something? The iPhone seems as it handles this much better.
Android apps have a hard Java heap limit which varies between devices. 24MB is a typical amount.
So the obvious workaround is to not allocate your big objects in Java... you can malloc your pixel byte arrays from a native C method instead.
However 24MB ought to be enough for anybody, to borrow a phrase, so I recommend you try to rethink your approach too. Perhaps be more aggressive about reusing bitmaps from a fixed-size pool, break your images up into smaller tiles, etc etc.
Avoid using getPixel() and setPixel() too much, it hence results in a really really bad performance, it's already mentioned on Android Documentation.
Also, manage your own memory usage Java, Garbage Collector will function as long as you follow the rule.