views:

92

answers:

3

From the docs:

Note: Core Data avoids the term unfaulting because it is confusing. There's no “unfaulting” a virtual memory page fault. Page faults are triggered, caused, fired, or encountered. Of course, you can release memory back to the kernel in a variety of ways (using the functions vm_deallocate, munmap, or sbrk). Core Data describes this as “turning an object into a fault”.

Is a Fault in Core Data essentially a memory page fault? I have only a slight idea about what a memory page is. I believe it's a kind of "piece of code in memory" which is needed to execute procedures and stuff like that, and as the app is runing, pieces of code are sucked into memory as "pages" and thrown away as they're not needed anymore. Probably 99% wrong ;)

Anyone?

+2  A: 

While your program is running not all of it stored on the RAM.
There are paging mechanisms as part of the OS that stores only the most used memory page in the RAM for faster access.
When you program trying to access a memory location that is not currently loaded to RAM a page fault occur and the page is brought from the disk.
Since the RAM available is limited only the most important memory pages stored in RAM.

This mechanism is subject to OS implementation.
For more comprehensive exploration of operating systems theory I would recommend the book Modern Operating Systems by A.S.Tenenbaum.

Itay
+2  A: 

A memory page is the basic unit of data for your application from the operating system's perspective. All of your code and data is arranged in groups of pages. When your program references a legal memory location within your application - either by attempting to read or write data or load an instruction, the address is translated into a location in one of these pages. A page fault occurs when the page holding the address is not actually present in physical memory. At that point the operating system needs to load the page from disk into memory so that your program can continue.

tvanfosson
So the page is loaded into memory automatically, when the pointer points to a location that's currently not loaded?
dontWatchMyProfile
Yes - it is loaded by the OS. When your application consumes too much memory, for example there are a lot of page faults that make the control go back to the kernel in order to read the pages from the disk and usually cause the program to be very slow.
Itay
is that related to the so called "swap file"?
dontWatchMyProfile
@dont - The swap file is where the OS (if it uses one) would store pages that it needs to remove from memory temporarily to make room for new pages in memory. Read-only pages (code) are not necessarily stored in the swap file as they can, if desired, be read from the original application. Of course, this all depends on the system implementation. You should look at the wikipedia article on paging, http://en.wikipedia.org/wiki/Paging, or pick up Tanenbaum's book for more info: http://en.wikipedia.org/wiki/Andrew_S._Tanenbaum
tvanfosson
+4  A: 

I have only a slight idea about what a memory page is. I believe it's a kind of "piece of code in memory" which is needed to execute procedures and stuff like that, and as the app is runing, pieces of code are sucked into memory as "pages" and thrown away as they're not needed anymore.

Stuff (i.e. code and data) exist in memory.

Each thing which exists in memory has an address (a memory address).

The memory address space (e.g. 4GB on a 32-bit machine) is divided into 'pages', where each page is a contiguous chunk of memory (e.g. 4KB per page).

The address space is mapped (by the CPU and the O/S) into RAM (or possibly mapped to I/O ports, but that's a different story).

There may be less RAM installed (e.g. 1 GB) than there is address space (e.g. 4 GB), therefore some stuff (e.g. the least-recently-used stuff) may be swapped out (by the O/S) from RAM onto a page file on disk. Whole, integral pages (e.g. 4KB chunks) are what's swapped (not individual bytes).

When the application tries to access an address which isn't currently mapped to RAM, then that's a so-called page fault. To handle a page fault, the O/S might:

  • Free some RAM, by swapping something (e.g. least-recently-used) from RAM to disk
  • Map that newly-freed now-available RAM to the address which the application is trying to access
  • Swap into RAM, from disk, whatever is supposed to be at that address (which at some time in the past had been swapped out from that address to disk)
  • Resume the application where it left off: the application tries again to access that memory address, only this time without another page fault.
ChrisW
I don't mind you glossing over the whole virtual memory thing (as did I), but given the level of detail you've given, don't you think you should mention it? The way you describe it, it sounds like each application is mapping to physical memory. Generally, what's swapped out isn't something tied to a physical address but rather another (virtual) page of your own application. No need for your application's memory usage characteristics to affect anyone else's application.
tvanfosson
@tvanfosson It didn't occur to me to mention it, and now that you have suggested it I am not sure how to summarise it. But in a way each application *is* mapping to physical memory: each application sees the whole (or most of the) address space as if the address space were owned by that application alone, and the O/S maps that address space to RAM. Multiple application implies multiple address space instances, and the different instances compete with each other for RAM.
ChrisW
Deciding what to swap out (e.g. whether to choose a page being used by this application, or by another application) is an O/S-specific implementation detail (and I don't know what the details are for the iPhone).
ChrisW