Jainendra
Jainendra

Reputation: 25143

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?

Upvotes: 4

Views: 323

Answers (3)

Linh Nguyen
Linh Nguyen

Reputation: 1264

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 :)

Upvotes: 0

Sid
Sid

Reputation: 14916

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));

Upvotes: 0

AndroidEx
AndroidEx

Reputation: 15824

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

Upvotes: 1

Related Questions