- The active list contains pages that are currently mapped into memory and have been recently accessed.
- The inactive list contains pages that are currently resident in physical memory but have not been accessed recently. These pages contain valid data but may be released from memory at any time.
- The free list contains pages of physical memory that are not associated with any address space of VM object. These pages are available for immediate use by any process that needs them.
Paging Virtual Memory Out
The kernel continuously compares the number of physical pages in the free list against a threshold value. When the number of pages in the free list dips below this threshold, the kernel reclaims physical pages for the free list by swapping inactive pages out of memory. To do this, the kernel iterates all resident pages in the active and inactive lists, performing the following steps:
- If a page in the active list is not recently touched, it is moved to the inactive list.
- If a page in the inactive list is not recently touched, the kernel finds the page’s VM object.
- If the VM object has never been paged before, the kernel calls an initialization routine that creates and assigns a default pager object.
- The VM object’s default pager attempts to write the page out to the backing store.
- If the pager succeeds, the kernel frees the physical memory occupied by the page and moves the page from the inactive to the free list.
Allocating and Accessing Virtual Memory
Applications usually allocate memory using the malloc routine. This routine finds free space on an existing page or allocates new pages using vm_allocate to create space for the new memory block. Through the vm_allocate routine, the kernel performs a series of initialization steps:
- It maps a range of memory in the virtual address space of this process by creating a map entry; the map entry is a simple structure that defines the starting and ending addresses of the region.
- The range of memory is backed by the default pager.
- The kernel creates and initializes a VM object, associating it with the map entry.
At this point there are no pages resident in physical memory and no pages in the backing store. Everything is mapped virtually within the system. When a program accesses the region, by reading or writing to a specific address in it, a fault occurs because that address has not been mapped to physical memory. The kernel also recognizes that the VM object has no backing store for the page on which this address occurs. The kernel then performs the following steps for each page fault:
- It acquires a page from the free list and fills it with zeroes.
- It inserts a reference to this page in the VM object’s list of resident pages.
- It maps the virtual page to the physical page by filling in a data structure called the pmap. The pmap contains the page table used by the processor (or by a separate memory management unit) to map a given virtual address to the actual hardware address.
最终的结论就是：mac系统中的可用内存大小是free + 部分inactive + 部分active，其中“部分inactive”和“部分active”都指“最近未使用的那部分”。而至于“最近使用”的部分应该隐含了两层意义：一、正在被某进程使用；二、之前使用该片内存页的进程已退出，但可能在被降级（active -> inactive，inactive -> free）之前该程序又启动新的进程，重复使用之前的内存页
-- EOF --