“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.
h/t: iPhoneKicks!Continue Reading →