4

When I tried to allocate more than approx. 30MB on Android 2.3 (an Samsung Galaxy 1) for my graphhopper project I run into OutOfMemory errors. But I recognized that for applications like Firefox and skobbler on the same device it is somehow possible to have more than 80MB allocated! At least I saw used memory of 90 or even 120MB in the task manager!

I found that newer Android versions >= 3 allow to set the big heap flag (largeHeap=true), but how are the Firefox guys doing this for Android 2.3?

Karussell
  • 17,085
  • 16
  • 97
  • 197
  • There is no "task manager" in Android. – CommonsWare Feb 09 '13 at 19:51
  • Hmmh, but I have one - maybe Samsung software? I'll check – Karussell Feb 09 '13 at 20:24
  • 1
    My point is that you have no idea what this "task manager" is actually measuring. Dianne Hackborn wrote [the definitive answer on measuring memory consumption](http://stackoverflow.com/questions/2298208/how-to-discover-memory-usage-of-my-application-in-android/2299813#2299813), and you will note that she did not rely upon random "task manager" apps from random pieces of hardware. – CommonsWare Feb 09 '13 at 20:29
  • Ok, that is a good point. (BTW: the task manager is an app from samsung) – Karussell Feb 09 '13 at 20:31

1 Answers1

2

I think it could be that those applications are using native memory which can exceed the limit (they request memory from native code).

It should be possible to do that even without native code via using a ByteBuffer and call allocateDirect. This could be validated using this hack.

Update Sadly this is only possible via native code according to this post. They've wrapped that in a java utility call. But for the commenter 'Delyan' suggests the following Java-only hack for image-processing:

BitmapFactory.Options opts = new BitmapFactory.Options();
Field field = opts.getClass().getField("inNativeAlloc");
field.setBoolean(opts, true);

but: "That said, bear in mind that this is dangerous. If the device runs out of memory, the oomkiller is going after you first. No warning, no nothing, just SIGKILL. So, more than ever, recycle everything you don't need and be very, very careful!"

Update 2 Eventually we can hack via reflections to access sun.misc.Unsafe as done in the popular Java-Chronicle project?

Community
  • 1
  • 1
Karussell
  • 17,085
  • 16
  • 97
  • 197
  • 2
    Bear in mind that "inNativeAlloc" does not exist in newer framework.jars. There's another thing you can do, if you know the Bitmap size in advance. You can make a blank png of the same size (BitmapFactory will put it off the Java heap), decode it and force the "mIsMutable" flag to be true. Then, you can draw over it. Unfortunately, you need to know the pixel size in advance. :/ Source - I've spent hours on this. Also, I'm the guy from that webpage. – Delyan May 03 '13 at 12:19