2

I'm looking into virtual memory management with android ndk and was wondering if there is an equivalent to the VirtualAlloc/Free family of functions in the android ndk, and if so, where can I find some docs on them.

Thank you for your time.

JaimeBarrachina
  • 430
  • 8
  • 21

1 Answers1

2

On Android, all memory allocations are "virtual", in the sense that the memory is not committed unless it is really accessed. If I remember correctly, VirtualAlloc comes from Windows CE, where the memory per process was artificially limited to 32 MByte (see http://denniskrabbe.wordpress.com/2009/10/09/windows-mobile-heaps-from-the-large-memory-area/). On Android, the limitations are the natural ones of a 32-bit architecture (iOS has recently entered the 64-bit era). There are some limitations on the JVM heap size, but they do not apply to native (NDK) memory (see Maximum native memory that can be allocated to an android app)

In Android, just like any Linux, malloc is optimistic, which means you can encounter out-of-memory after your malloc succeeded, see Does malloc lazily create the backing pages for an allocation on Linux (and other platforms)?. On the other hand, bionic does not provide custom memory allocation hooks as glibc.

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • Does this mean that I should be using malloc/new to handle memory? In VirtualAlloc I can request to reserve or commit explicitly, how do I do this in android native? – JaimeBarrachina Nov 01 '13 at 14:35
  • 1
    What are you trying to do? (I'm feeling a little http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem here.) – fadden Nov 01 '13 at 20:16
  • 1
    No, in Android (neither in bionic nor in the Linux kernel) one can not request to reserve or commit explicitly. – Alex Cohn Nov 02 '13 at 13:19