Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does mmap fail on iOS?

I'm trying to use mmap to read and play audio files on iOS. It works fine for files up to about 400MB. But when I try a 500MB file, I get a ENOMEM error.

char *path = [[[NSBundle mainBundle] pathForResource: @"test500MB" ofType: @"wav"] cStringUsingEncoding: [NSString defaultCStringEncoding]];
FILE *f = fopen( path, "rb" );
fseek( f, 0, SEEK_END );
int len = (int)ftell( f );
fseek( f, 0, SEEK_SET );

void *raw = mmap( 0, len, PROT_READ, MAP_SHARED, fileno( f ), 0 );

if ( raw == MAP_FAILED ) {
    printf( "MAP_FAILED. errno=%d", errno ); // Here it says 12, which is ENOMEM.
}

Why?

I'd be happy with an answer like "700MB is the virtual memory limit, but sometimes the address space is fragmented, so you DO get 700MB but in smaller chunks". (This is just speculation, I still need an answer)

The Apple doc page about virtual memory says:

Although OS X supports a backing store, iOS does not. In iPhone applications, read-only data that is already on the disk (such as code pages) is simply removed from memory and reloaded from disk as needed.

which seems to confirm that mmap should work for blocks larger than the physical memory but still doesn't explain why I'm hitting such a low limit.

Update

  • This answer is interesting, but 500MB is well below the 700MB limit it mentions.
  • This discussion mentions contiguous memory. So memory fragmentation could be a real issue?
  • I'm using iPod Touch 4th generation which has 256MB physical memory.
  • The point of my research is to see if there's a better way of managing memory when loading read-only data from files than "keep allocating until you get a memory warning". mmap seemed like a nice way to solve this...

Update 2

I expect mmap to work perfectly with the new 64bit version of iOS. Will test once I get my hands on a 64bit device.

like image 297
Tomas Andrle Avatar asked Nov 16 '12 22:11

Tomas Andrle


1 Answers

After further investigation and reading this excellent blog post by John Carmack, here are my conclusions:

  • 700MB is the limit for virtual memory on iOS (as of 2012, 32-bit iOS)
  • It may or may not be available in a single block; this depends on device state and app behaviour

Therefore, to reliably mmap 700MB worth of file data it is necessary to break it into smaller chunks.

like image 195
Tomas Andrle Avatar answered Sep 24 '22 07:09

Tomas Andrle