Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Library for unrestricted heap memory for bitmaps using NDK on Android [closed]

By design, Android apps are very limited in the amount of heap memory they can use. The limitation for SDK apps is as little as 16MB on old devices. This design choice usually makes sense because the OS tries to support multi-tasking on devices which are usually very low on memory - so each task gets its own small ration.

The memory limitation varies per device. On stock Samsung Galaxy S2 for example, each app gets somewhere between 32MB-64MB. This device has 1GB of RAM, so a single task can only use about 5% of what the device can offer.

It's worth to mention that memory strategy on iOS is very different. To the best of my knowledge, there is no externally enforced limit on how much memory an iOS app can use. You can allocate as much as you want until the system runs out of memory. If you're too greedy, other background apps will be terminated.

Let's get this out of the way - this isn't a discussion about which memory strategy is better.

The memory limitation on Android is hard to swallow for certain types of apps. Graphic-intensive apps (such as games) need lots of memory to hold bitmaps. Even a web browser should be pretty hard to implement under the default limit.

When resource optimization isn't enough, the standard strategy for overcoming the memory limit is using the NDK. The "native" heap in Android doesn't have artificial limitations and behaves much like the heap on iOS. Using the "native" heap to hold bitmaps works well - this is actually how bitmaps were kept on Android 2.3.3 and lower (the JVM still limited them artificially though).

I'm looking for an NDK+JNI library to hold unlimited bitmaps in the "native" heap. Its Java interface should be identical to the stock bitmap API. I'm not aware of any public project for this purpose and will appreciate your help to find one. If you've implemented this before and willing to share your code, it will be appreciated as well.

Final comment: Please do not make this a discussion about the morality of having such a library available to the general public.

like image 886
talkol Avatar asked Oct 21 '22 03:10

talkol


1 Answers

The heap limit on recent 2GB devices is around 192MB. Apps that need a large amount of memory, such as image editors, can include android:largeHeap=true in the app manifest to increase that to a higher limit (currently around 512MB).

Working around the limits is generally a bad idea. It's calibrated on each device based on the amount of physical memory, the display size, and how the device performs when it has a bunch of apps running. Remember that, on Android, your app isn't the only thing present, and not all of physical memory is reserved exclusively for apps (a big piece goes to the kernel, buffers for multiple frames of graphics and video content, and so on).

The bigger your app is, the more likely it is to be killed. The kernel tries very hard to not kill the foreground app, which means you're going to start squeezing out background processes -- or cause them to do so much paging and restarting that it starts to impact the performance of your game.

Having said all that, I think you're looking for this.

like image 197
fadden Avatar answered Oct 24 '22 04:10

fadden