Android's per-application max heap size - android

Android has a heap size limit for each app. It could be 16M, 24M, 32M, etc.
My question is, will the size of the app itself is taken into account in the heap size limit?
For example, suppose the Android max heap size per-app is 16M. And my app is 10M bytes in size. Does it mean the OS will only leave the app 6M bytes heap after the app is running?
Thanks.

No. Your app is made of classes and drawable that will be loaded into memory at run time only when you use it. So for example let's say your app contains drawable that are for tablets, they are not loaded into the memory of a phone.

No, you'll have all 16M of heap.
Things that eat the heap are classes loaded in RAM, and objects created in RAM during the app session.

Related

What is a safe heap size?

I have created a simple game with libgdx. It's currently showing about 50mb heap size. I believe everything is being disposed, and I try not to create objects in redner loop. What is a safe heap size?
From developer.android.com
...each Android-powered device has a different amount of RAM available to the system and thus provides a different heap limit for each app. You can call getMemoryClass() to get an estimate of your app's available heap in megabytes. If your app tries to allocate more memory than is available here, it will receive an OutOfMemoryError.

When Android gives OutOfMemory Exception?

I was reading about OutOfMemory error in Android, which comes when Android runs out of memory. Do we know that if my app consumes some x MB of memory then it will give OutOfMemory error? Does Android allocates a specific amount of memory to each app depending on the device and total physical memory?
For example, I've a device with 2GB RAM installed, 500MB is taken by OS, 500 MB is taken by other apps. Now my app has 1048MB of memory to run. So in this particular case when the system gives OutOfMemory?
Each app has some memory limit it can utilize for heap allocations. It differs for different phones (and you can increase it in manifest as well). This answer provides a great detail on this, giving specific figures for some models and settings.
As for how it is determined:
it tends to be based more on screen resolution, as higher-resolution screens tend to want to manipulate larger bitmaps, so Google makes heap size recommendations that, hopefully, device manufacturers will abide by.
CommonsWare
What you are looking for is best described by google itself Here
To get how much memory you can use you could do this:
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
int memoryClass = manager.getMemoryClass();
Log.v("onCreate", "memoryClass:" + Integer.toString(memoryClass));
your OutOfMemory Exception caused by Heap size , not about the RAM .
To maintain a functional multi-tasking environment, Android sets a
hard limit on the heap size for each app. The exact heap size limit
varies between devices based on how much RAM the device has available
overall. If your app has reached the heap capacity and tries to
allocate more memory, it will receive an OutOfMemoryError.
here an useful link : https://developer.android.com/training/articles/memory.html
hope that help you :)

Android Low memory on start

For several apps that I have tested, it seems that the Heap memory usage is quite high even at the start of the application. For example, I have an app that is supposed to show an image from assets on an ImageView. But, even before loading the image, used Heap, is about 95%. When I load and display the image, it still fine, it increases used memory to about 98%. But, if I create a copy of the bitmap, then app crashes with out of memory error.
Again, my main question is why the amount of free memory is so low right at the start of an application, before any processing is done.
The reason you are seeing such high amount of memory usage percentage is because the heap hasn't grown to its maximum size. This means that even though you see 98% of usage that refers to your current heap size and not the total amount that you can use.
At runtime, the heap grows dynamically as Dalvik VM needs more memory. It is common that you start with a relatively small heap and with each GC if needed Dalvik will request more memory.
Now for your OutOfMemoryError it can be a lot of things. I would suggest sampling your bitmap at runtime using the BitmapFactory class inSampleSize

Maximum Heap size for Nexus?

I am using Galaxy Nexus(i9250) for development and testing. I noticed strange fact- sometimes when the total heap size is 64mb and allocated heap size is in and around 56-60mb, app crashes. But sometimes I noticed that even the memory shoots up to 80mb, app didn't crashes.
Initially I thought that maximum heap size for devices of the range nexus will be 64mb(now I realize it is wrong). So my question is what is the maximum heap size a device can use. If it is variable based on device, then on what factor heap size depends. I knew this is a common question. Could anyone guide me to the right answer. Thanks in advance!
NOTE: I didn't use LargeHeapSize = true; in my code
Considering , your app crashes every time giving "OutOfMemoryError"
DVM starts by allocating a small heap, after every garbage collection cycle it checks for free heap and compares it to the total heap memory and adds more memory in case that the difference is too small. So Heap can be increased or decreased at runtime as per Dalvik VM's requirement by OS.
So, when their is enough memory available in the system your app didn't get crashed when the memory shoots up to 80mb.(Assuming 64MB is not the hard limit of heap size)
Yes there is a hard limit on the heap size of every device which can be increased by using "LargeHeapSize = true" , but this might be too costly in terms of app performance as increased heap size is directly proportional to the time taken by the garbage collection process(as GC now have to traverse more objects inside bigger heap). So it is a big "NO,NO" until unless you exactly know what and why are you going for larger heap size.
on what factor heap size depends:
Heap size mainly depends on the resolution of the device you are
using, as more resolution needs larger images/bitmaps size to fit
in.(more pixels = needs more memory to incorporate)
Although, I didn't got through any written proof of this.. As per my
understanding Heap size also depends on the RAM size. Since, bigger
RAM size will allow more flexibility of multitasking and lesser
chances of getting the "OutOfMemoryError"
In case you needs to know exact amount of heap memory you can use, ActivityManager.getMemoryClass(). This method returns an integer indicating the number of megabytes available for your app's heap
ActivityManager.getLargeMemoryClass() for larger heap size
it is not clear that what device you are exactly talking about. It is also not clear how you calculated your heap memory.
I recommend you calculate your heap memory and available memory using this link
But if your app uses Native Memory, their are no restriction on that link.
I would only use the DDMS values as a guide to find memory leaks and memory allocation problems rather than some specific number given that you can target. Any Android application will be expected to run on a variety of devices so while you may have tuned your app for a 'Galaxy Nexus' you will want to be able to run on older devices, and test appropriately. See
#dongshengcn comment.
In addition to the links by #minhaz I would also read: Understanding Heap Size.
If you are trying to get a better understanding of memory management on Android then you should read Android Framework Engineer #hackbod's answer to: How to Discover Memory Usage of My Application in Android.

Difference in garbage collector behavior on Android device versus the emulator

I'm testing my application's memory usage on the emulator. And the problem is that on the emulator the app heap is just growing and growing, just a little bit of resources are freed. And if no collections are made it will cause an OutOfMemory exception on big resolution screens.
I downloaded the Sony SDK and there is an emulator configuration for the Xperia Z that has 1080x1920 resolution and the default heap is 64MB. I think it's a small heap size for that resolution because my app uses 40MB only starting up. However on my phone it's using 15MB of 64MB (res. 540x960).
So this quite small heap size (might not be real?) + GC behavior is causing OutOfMemory quite fast.
On a real device (I've tested only on mine), GC is working very nicely, it's freeing resources that are no longer used, but I really cannot predict if that will work on other phones.
Should I ignore how GC is working on my emulator or might it be my app's problem?
Growing heap on emulator indicates that at some point you have memory leak.
They are very common when you send intents between different applications ( e.g select image from gallery) . Most of device can handle such leaks with no problem.
Another reason for heap to grow up: inefficient memory operations. That means at some time you are asking to much memmory ( e.g you selected 5M image from gallery, created inpuststream for it and keep in memory as bitmap, so you asking 15+M emulator will show just hight heap grow, but most of devices will show error).
If you see heap grows - analyse your memory usage and detect leaks. Link
If you dont detect anything strange you can almost safely ignore heap warning.
Note: heap show supplied space, not used.

Categories

Resources