
In OS X and in earlier versions of iOS, the size of a page is 4 kilobytes. In iOS, there is no backing store and so pages are are never paged out to disk, but read-only pages are still be paged in from disk as needed. Moving data from physical memory to the backing store is called paging out (or “swapping out”) moving data from the backing store back in to physical memory is called paging in (or “swapping in”). The backing store is a disk-based repository containing a copy of the memory pages used by a given process. In OS X, the virtual memory system often writes pages to the backing store. How the system release pages depends on the platform.
OSX FREE MEMORY FREE
If there are no free pages available in physical memory, the handler must first release an existing page to make room for the new page. The page-fault handler stops the currently executing code, locates a free page of physical memory, loads the page containing the needed data from disk, updates the page table, and then returns control to the program’s code, which can then access the memory address normally. When that happens, the virtual memory system invokes a special page-fault handler to respond to the fault immediately. However, if an application accesses an address on a memory page that is not currently in physical RAM, a page fault occurs.


This translation occurs automatically and is transparent to the running application.Īs far as a program is concerned, addresses in its logical address space are always available.
OSX FREE MEMORY CODE
When a program’s code accesses an address in memory, the MMU uses the page table to translate the specified logical address into the actual hardware memory address. The processor and its memory management unit (MMU) maintain a page table to map pages in the program’s logical address space to hardware addresses in the computer’s RAM. The virtual memory manager creates a logical address space (or “ virtual” address space) for each process and divides it up into uniformly-sized chunks of memory called pages. Virtual memory allows an operating system to escape the limitations of physical RAM. For more detailed information on how the virtual memory system works, see Kernel Programming Guide. The following sections introduce terminology and provide a brief overview of the virtual memory system used in both OS X and iOS. Instead, it uses all of the available space on the machine’s boot partition. Note: Unlike most UNIX-based operating systems, OS X does not use a preallocated disk partition for the backing store.

Applications that fail to free up enough memory are terminated. 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. Writable data is never removed from memory by the operating system. 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. 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.Īlthough OS X supports a backing store, iOS does not. 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. To give processes access to their entire 4 gigabyte or 18 exabyte address space, OS X uses the hard disk to hold data that is not currently in use. Even for computers that have 4 or more gigabytes of RAM available, the system rarely dedicates this much RAM to a single process. In addition, OS X provides approximately 18 exabytes of addressable space for 64-bit processes. Both systems also provide up to 4 gigabytes of addressable space per 32-bit process. In order to properly tune your code though, you need to understand something about how the underlying system manages memory.īoth OS X and iOS include a fully-integrated virtual memory system that you cannot turn off it is always on.

Minimizing memory usage not only decreases your application’s memory footprint, it can also reduce the amount of CPU time it consumes. Next Previous About the Virtual Memory SystemĮfficient memory management is an important aspect of writing high performance code in both OS X and iOS.
