You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Developer Forums relocated!

Need help with Apple Developer tools and technologies? Want to share information with other developers and Apple engineers? Visit Developer Forums at Apple.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Does iOS support virtual memory and a swap file?

I'm getting conflicting reports on this one...

MacBook Pro 17", 2.66 GHz i7, 4GB 1067 DDR3 RAM, OS X 10.6.4 /// iPhone 4, 32GB, iOS 4

Posted on Jan 24, 2011 3:43 PM

Reply
Question marked as Top-ranking reply

Posted on Jan 24, 2011 4:28 PM

From +Memory Usage Performance Guidelines+
Both Mac OS X and iOS include a fully-integrated virtual memory system that you cannot turn off; it is always on. Both system also provide up to 4 gigabytes of addressable space per 32-bit process. In addition, Mac OS X provides approximately 18 exabytes of addressable space for 64-bit processes. Even for computers that have 4 or more gigabytes of RAM available, the system rarely dedicates this much RAM to a single process.


To give processes access to their entire 4 gigabyte or 18 exabyte address space, Mac OS X uses the hard disk to hold data that is not currently in use. As memory gets full, sections of memory that are not being used are written to disk to make room for data that is needed now. The portion of the disk that stores the unused data is known as the backing store because it provides the backup storage for main memory.


Although Mac 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. Writable data is never removed from memory by the operating system. Instead, if the amount of free memory drops below a certain threshold, the system asks the running applications to free up memory voluntarily to make room for new data. Applications that fail to free up enough memory are terminated.
8 replies
Question marked as Top-ranking reply

Jan 24, 2011 4:28 PM in response to Den B.

From +Memory Usage Performance Guidelines+
Both Mac OS X and iOS include a fully-integrated virtual memory system that you cannot turn off; it is always on. Both system also provide up to 4 gigabytes of addressable space per 32-bit process. In addition, Mac OS X provides approximately 18 exabytes of addressable space for 64-bit processes. Even for computers that have 4 or more gigabytes of RAM available, the system rarely dedicates this much RAM to a single process.


To give processes access to their entire 4 gigabyte or 18 exabyte address space, Mac OS X uses the hard disk to hold data that is not currently in use. As memory gets full, sections of memory that are not being used are written to disk to make room for data that is needed now. The portion of the disk that stores the unused data is known as the backing store because it provides the backup storage for main memory.


Although Mac 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. Writable data is never removed from memory by the operating system. Instead, if the amount of free memory drops below a certain threshold, the system asks the running applications to free up memory voluntarily to make room for new data. Applications that fail to free up enough memory are terminated.

Jan 24, 2011 5:03 PM in response to xnav

OK, so that is a confusing part. I understand about Mac's, they do have both. But an iPhone or an iPod touch has only 4GB of virtual space, right? (Even though the actual amount of RAM may be 120mb.) If so, does it mean that my app can "hypothetically" allocate 1GB of space in perfect conditions?

Also, from what it said above, iOS does not have a swap file, right? And instead the iOS asks running apps to free memory, or else... (they'll be terminated).

Jan 24, 2011 7:38 PM in response to xnav

OK, guys, what I don't understand is this concept. Say, I write an app that uses RAM extensively. When someone starts it up the iOS may ask other suspended apps to release memory (because of my glutenous app). So I get this part. But then, if there's still not enough memory available, my own app can be asked to release memory when it's running on the foreground, right? If so, how can it function if upon its start-up the iOS tells it to free memory which it may clearly need. (Again, I'm not saying that the app I'm writing does this, but let's say if it happens to run on an older device that has very little RAM. What would happen to it then?)

In other words, is there any way to know the limit (or percentage) of RAM that an app can reserve and continue running w/o risking to be terminated even from the foreground?

Jan 24, 2011 7:53 PM in response to Den B.

Hey! You are the same person asking about initWithCapacity in the other thread. Now we know what you are up to 🙂

What is taking so much memory? You have a fast processor and fast storage. Just load your data on demand. If it doesn't run fast enough, create a simple caching mechanism. I bet that loading your data dynamically is going to yield better performance than asking the OS to shuffle everything around.

Jan 24, 2011 9:48 PM in response to etresoft

etresoft wrote:
Hey! You are the same person asking about initWithCapacity in the other thread. Now we know what you are up to 🙂

Oh no, you got me 🙂

etresoft wrote:
What is taking so much memory? You have a fast processor and fast storage. Just load your data on demand. If it doesn't run fast enough, create a simple caching mechanism. I bet that loading your data dynamically is going to yield better performance than asking the OS to shuffle everything around.


It may be true, I was just hoping to go the "easy round"

Jan 24, 2011 9:55 PM in response to Den B.

Yea, I was wondering the same. Everything I have read suggests executable code is paged, and the iOS supports swapping but that it is disabled to avoid thrashing the Flash storage, and the app is expected manage its own resources which makes sense and is probably a good idea (since you know what mem is readonly/readwrite, and what should be specifically cached when..). There is also NSData initWithContentsOfMappedFile: which may be of use in some situations. But I also want to know how much actual RAM I can use, and if having many other BG Apps decreases this. I tried the following code:

for (int i=1; malloc(1024); i++)
if (!(i % 1024))
printf("malloc: %f MB\n", i/1024.);

which returns ~600 MB on both iPod 3rd and 4th gen (and regardless of if 100 apps are in BG or none). But both of these devices should be only 256 MB, so I must have something wrong in this code right?

Does iOS support virtual memory and a swap file?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.