Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does mmap fail on iOS?
    primarykey
    data
    text
    <p>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.</p> <pre><code>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. } </code></pre> <p><strong>Why?</strong></p> <p>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)</p> <p>The Apple doc page about virtual memory says:</p> <blockquote> <p>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.</p> </blockquote> <p>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. </p> <p><strong>Update</strong></p> <ul> <li><a href="https://stackoverflow.com/questions/9184773/is-there-a-practical-limit-on-the-number-of-memory-mapped-files-in-ios">This answer</a> is interesting, but 500MB is well below the 700MB limit it mentions.</li> <li><a href="https://stackoverflow.com/questions/726471/how-big-can-a-memory-mapped-file-be?rq=1">This discussion</a> mentions <strong>contiguous memory</strong>. So memory fragmentation could be a real issue?</li> <li>I'm using iPod Touch 4th generation which has 256MB physical memory.</li> <li>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 <em>"keep allocating until you get a memory warning"</em>. <code>mmap</code> seemed like a nice way to solve this...</li> </ul> <p><strong>Update 2</strong></p> <p>I expect mmap to work perfectly with the new 64bit version of iOS. Will test once I get my hands on a 64bit device.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload