Under the Bridge

Tip: iPhone Virtual Memory

“What?” you’re no doubt thinking, “is he going on about? Everyone knows there’s no virtual memory on the iPhone!”

Ah, young padawan, let us correct your misconception. It turns out that, indeed, there actually is. Take a look through this moderately amusing rant about the indeterminacy of memory allotments on the iPhone and into the comments. There’s a variety of suggestions about killing other processes, hogging memory to your own process, and in general going out of your way to not play nice with others; which, well, that’s not the iPhone way, is it now? But then, there’s this piece of solid gold:

There actually is a virtual memory system pager. However you have to manually set it up.
Search for ‘mmap’ in Apple’s iPhone Developer Forum and you’ll find a very informative thread.
Basically, it works like this: In place of malloc, you create a file of the appropriate size and then memory map that file using mmap. The virtual memory pager will page in and out the memory pages as needed. This works both for read-only data (you can just use [NSData dataWithContentsOfMappedFile: ] for that) as well as read-write memory.

Woah. We were utterly and completely unaware that was supported on the iPhone. mmap FTW!

h/t: iPhoneKicks!

3
  • http://iphone.dailynewsabout.com/archives/109 Daily News About Iphone : A few links about Iphone – Thursday, 14 May 2009 23:12

    [...] Tip: iPhone Virtual Memory [...]

  • Sherif Omran

    It is really nice and important to use memory mapping (virtual memory) in the iphone so that we can run many applications at the same time.

    Unfortunately i cannot login to apple forums, could you please copy their instruction and place them here?

    Thanks.

  • http://www.alexcurylo.com/ Alex

    Sorry, Sherif, that would be a violation of the terms of service of the developer boards, and we won’t do that. We *definitely* won’t do that in public :)

    That said, I trust non-violating instructions can be summed up as “you can use mmap when running on the iPhone just the same way as you use it when not running on the iPhone”. So man mmap is your friend.