Reputation: 101
I am developing an Android application. Is it possible to allocate RAM for that application? For example: When my application opens it must allocate 30MB RAM. Mention the APIs.
Upvotes: 1
Views: 9721
Reputation: 14093
No you can't. This is entirely managed by the system.
In fact, the system will grow the heap allocated to your applicaton anytime it needs it. However, there is a limit by applicaton regarding memory consumption. To retrieve this limit, you can use the following :
/* Expressed in Megabytes */
Runtime.getRuntime().maxMemory() / 1024 / 1024
Finally, it appears you can specify in your manifest that your application is likely to be using more memory than a regular app by adding :
If you use this tag, look at ActivityManager.getLargeMemoryClass() to verify the values assigned to your app. The returned value is expressed in megabytes. It can be higher than the value returned by ActivityManager.getMemoryClass(), given that your device's memory availabilty allows it.
Upvotes: 5
Reputation: 11185
No you cannot do that. Memory allocated to an application is specific to the device. I'd highly recommend that you watch memory management video from Google IO 2011 to gain a deeper understanding of this topic.
Additionally take a look at the debug logs from logcat to understand how much memory is allocated to your app and how much of it remains.
01-13 14:37:54.538: D/dalvikvm(27390): GC_CONCURRENT freed 393K, 47% free 3377K/6279K, external 2003K/2199K, paused 2ms+3ms
I'd not recommend using largeHeap="true" either. Like @Halim noted in his post the additional memory may not be allocated to your app anyway.
Enabling this also does not guarantee a fixed increase in available memory, because some devices are constrained by their total available memory.
Focus on the need for 30MB of memory and how you can reduce it.
Upvotes: 0