Search code examples
androidmemorymemory-managementout-of-memoryandroid-memory

What is the maximum amount of RAM an app can use?


I am quite curious about this question concerning the memory management of the Android operating system so I hope for a quite detailed answer on that topic.

What I would like to know:

  • What is the maximum amount of memory (in megabytes / as percentage of the total RAM) that an Android application (that is not a system app) can use?
  • Are there any differences between Android versions?
  • Are there any differences concerning the manufacturer of the device?

And most importantly:

  • What is considered / what does it depend on when it comes to the system determining how much RAM an app can use at runtime (assuming that the memory maximum per app is not a static number)?

What I have heard so far (up until 2013):

  • Early Android devices had a per-app cap of 16MB
  • Later this cap increased to 24MB or 32MB

What makes me very curious:

Both of these limits are very low.

I have just recently downloaded the Android Task Manager to check my devices RAM. What I have noticed is that there are applications using around 40-50 megabytes of RAM, which is obvioulsy more than the mentioned maximum RAM usage of let's say 32 MB. So how does Android determine how much RAM an app can use? How is it possible that apps exceed that limit?

Furthermore, I noticed that some apps of mine crash (killed by the system?) with an OutOfMemoryException when using around 30-40 Megabytes. On the other hand, I have apps running on my phone using 100 MB and more after some time (probably due to memory leaks) that do not crash or get killed off. So it obviously also depends on the app itself when it comes to determining how much RAM can be spared. How is this possible? (I conducted my tests with an HTC One S with 768 MB RAM)

Disclaimer: I am NOT affiliated with Android Task Manager app in any way.


Solution

  • What is the maximum amount of memory (in Megabytes / as percentage of the total RAM) that an Android application (that is not a system app) can use?

    That varies by device. getMemoryClass() on ActivityManager will give you the value for the device your code is running upon.

    Are there any differences between the Android versions?

    Yes, insofar as OS requirements have increased over the years, and devices have to adjust to match.

    Are there differences concerning the manufacturer of the device?

    Yes, insofar as manufacturers manufacture devices, and the size varies by device.

    Which "side factors" are taken into consideration when it comes to determining how much RAM an app can use?

    I have no idea what "side factors" means.

    Early devices had a per-app cap of 16MB; Later devices increased that to 24MB or 32MB

    That's about right. Screen resolution is a significant determinant, as larger resolutions mean larger bitmaps, and so tablets and high-resolution phones will tend to have higher values yet. For example, you will see devices with 48MB heaps, and I wouldn't be surprised if there are values higher than that.

    How is it possible that apps exceed that limit?

    You assume that the author of that app knows what (s)he is doing. Considering that memory usage of an app is difficult for a core Android engineer to determine, I would not assume that the app in question is necessarily providing particularly accurate results.

    That being said, native code (NDK) is not subject to the heap limit. And, since Android 3.0, apps can request a "large heap", usually in the hundreds of MB range, but that's considered poor form for most apps.

    Furthermore, I noticed that some apps of mine crash with an OutOfMemoryException when using around 30-40 Megabytes.

    Bear in mind that the Android garbage collector is not a compacting garbage collector. The exception really should be CouldNotFindSufficientlyLargeBlockOfMemoryException, but that was probably deemed too wordy. OutOfMemoryException means that you could not allocate your requested block, not that you have exhausted your heap entirely.