1. 1. About Virtual Memory
  2. 2. Details of the Virtual Memory System
  3. 3. Wired Memory
  4. 4. Page Lists in the Kernel
  5. 5. Paging Out Process
  6. 6. Paging In Process
  7. 7. 名称解释
    1. 7.0.1. Free memory
    2. 7.0.2. Wired memory
    3. 7.0.3. Active memory
    4. 7.0.4. Inactive memory
    5. 7.0.5. Used
    6. 7.0.6. VM size
    7. 7.0.7. Page ins / Page outs
    8. 7.0.8. Swap used

这篇文章翻译下Memory Usage Performance Guidelines

About Virtual Memory

Virtual memory allows an operating system to escape the limitations of physical 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. 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. 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. This translation occurs automatically and is transparent to the running application.


As far as a program is concerned, addresses in its logical address space are always available. 就程序而言,其逻辑地址空间中的地址总是可用的。
However, if an application accesses an address on a memory page that is not currently in physical RAM, a page fault occurs.但是,如果应用程序访问当前不在物理RAM中的内存页上的地址,则会发生页面错误。 When that happens, the virtual memory system invokes a special page-fault handler to respond to the fault immediately.当发生这种情况时,虚拟内存系统调用一个特殊的页面错误处理程序来立即响应错误。 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. This process is known as paging. 页面错误处理程序停止当前执行的代码,定位物理内存的空闲页面,从磁盘装载包含所需数据的页面,更新页面表,然后返回对程序代码的控制,然后程序代码就可以正常地访问内存地址。这个过程称为分页。

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.如果在物理内存中没有可用的空闲页面,处理程序必须首先释放一个现有页面,以便为新页面腾出空间。 How the system release pages depends on the platform. 系统释放页面的方式取决于平台。In OS X, the virtual memory system often writes pages to the backing store.在OS X中,虚拟内存系统经常将页面写入后备存储器。 The backing store is a disk-based repository containing a copy of the memory pages used by a given process.后备存储是一个基于磁盘的存储库,其中包含给定进程使用的内存页的副本。 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”). 将数据从后台存储恢复到物理内存称为分页in(或“交换in”)。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.在iOS中,没有备份存储,所以不会将页面调出到磁盘,但是只读页面仍然根据需要从磁盘调入。

In OS X and in earlier versions of iOS, the size of a page is 4 kilobytes.在OS X和iOS的早期版本中,页面的大小是4千字节。 In later versions of iOS, A7- and A8-based systems expose 16-kilobyte pages to the 64-bit userspace backed by 4-kilobyte physical pages,在iOS的后续版本中,基于A7和基于a8的系统将16千字节的页暴露给由4千字节物理页支持的64位用户空间, while A9 systems expose 16-kilobyte pages backed by 16-kilobyte physical pages. 而A9系统将16千字节的页暴露给由16千字节物理页支持的64位用户空间。These sizes determine how many kilobytes the system reads from disk when a page fault occurs. 这些大小决定了当发生页面错误时系统从磁盘读取多少千字节。Disk thrashing can occur when the system spends a disproportionate amount of time handling page faults and reading and writing pages, rather than executing code for a program. 当系统花费过多的时间处理页面错误、读写页面,而不是为程序执行代码时,就会发生磁盘抖动。

Paging of any kind, and disk thrashing in particular, affects performance negatively because it forces the system to spend a lot of time reading and writing to disk. Reading a page in from the backing store takes a significant amount of time and is much slower than reading directly from RAM. If the system has to write a page to disk before it can read another page from disk, the performance impact is even worse.

Details of the Virtual Memory System

The logical address space of a process consists of mapped regions of memory. Each mapped memory region contains a known number of virtual memory pages. Each region has specific attributes controlling such things as inheritance (portions of the region may be mapped from “parent” regions), write-protection, and whether it is wired (that is, it cannot be paged out). Because regions contain a known number of pages, they are page-aligned, meaning the starting address of the region is also the starting address of a page and the ending address also defines the end of a page.

The kernel associates a VM object with each region of the logical address space.内核将VM对象与逻辑地址空间的每个区域关联起来。 The kernel uses VM objects to track and manage the resident and nonresident pages of the associated regions.内核使用VM对象跟踪和管理关联区域的驻留页和非驻留页。 A region can map to part of the backing store or to a memory-mapped file in the file system.区域可以映射到备份存储的一部分,或者映射到文件系统中的内存映射文件。 Each VM object contains a map that associates regions with either the default pager or the vnode pager. 每个VM对象都包含一个映射,该映射将区域与默认分页器或vnode分页器关联起来。The default pager is a system manager that manages the nonresident virtual memory pages in the backing store and fetches those pages when requested. 默认分页器是一个系统管理器,它管理后台存储中的非驻留虚拟内存页,并在请求时获取这些页。The vnode pager implements memory-mapped file access. vnode分页器实现内存映射文件访问。The vnode pager uses the paging mechanism to provide a window directly into a file. vnode分页器使用分页机制直接向文件提供一个窗口。This mechanism lets you read and write portions of the file as if they were located in memory.该机制允许您读取和写入文件的部分,就像它们位于内存中一样。

In addition to mapping regions to either the default or vnode pager, a VM object may also map regions to another VM object. The kernel uses this self referencing technique to implement copy-on-write regions. Copy-on-write regions allow different processes (or multiple blocks of code within a process) to share a page as long as none of them write to that page. When a process attempts to write to the page, a copy of the page is created in the logical address space of the process doing the writing. From that point forward, the writing process maintains its own separate copy of the page, which it can write to at any time. Copy-on-write regions let the system share large quantities of data efficiently in memory while still letting processes manipulate those pages directly (and safely) if needed. These types of regions are most commonly used for the data pages loaded from system frameworks.

Field Description
Resident pages A list of the pages of this region that are currently resident in physical memory.
Size The size of the region, in bytes.
Pager The pager responsible for tracking and handling the pages of this region in backing store.
Shadow Used for copy-on-write optimizations.
Copy Used for copy-on-write optimizations.
Attributes Flags indicating the state of various implementation detai**ls.

If the VM object is involved in a copy-on-write (vm_copy) operation, the shadow and copy fields may point to other VM objects. Otherwise both fields are usually NULL.

Wired Memory

Wired memory (also called resident memory) stores kernel code and data structures that must never be paged out to disk. Applications, frameworks, and other user-level software cannot allocate wired memory. However, they can affect how much wired memory exists at any time. For example, an application that creates threads and ports implicitly allocates wired memory for the required kernel resources that are associated with them.
Wired memory(也称为常驻内存)存储内核代码和数据结构,这些代码和数据结构永远不能被分页到磁盘上。应用程序、框架和其他用户级软件不能分配Wired memory。然而,它们可以影响在任何时候存在多少Wired memory。例如,创建线程和端口的应用程序隐式申请了Wired memory,这些内存是内核资源需要的。

Table 2 lists some of the wired-memory costs for application-generated entities.

Resource Wired Memory Used by Kernel
Process 16 kilobytes
Thread blocked in a continuation—5 kilobytes; blocked—21 kilobyte
Mach port 116 bytes
Mapping 32 byte
Library 2 kilobytes plus 200 bytes for each task that uses it
Memory region 160 bytes

As you can see, every thread, process, and library contributes to the resident footprint of the system. In addition to your application using wired memory, however, the kernel itself requires wired memory for the following entities:
如您所见,每个线程、进程和库都会影响系统的驻留内存。但是,除了使用Wired memory的应用程序之外,内核本身的以下实体会使用有线内存:

  1. VM objects
  2. the virtual memory buffer cache
  3. I/O buffer caches
  4. drivers

Wired data structures are also associated with the physical page and map tables used to store virtual-memory mapping information, Both of these entities scale with the amount of available physical memory. Consequently, when you add memory to a system, the amount of wired memory increases even if nothing else changes. When a computer is first booted into the Finder, with no other applications running, wired memory can consume approximately 14 megabytes of a 64 megabyte system and 17 megabytes of a 128 megabyte system.
Wired data structures还与物理页面、用于存储虚拟内存映射信息的映射表相关联,这两个实体都与可用物理内存的大小相关联。因此,当您向系统添加内存时,即使没有其他变化,wired-memory的数量也会增加。当一台计算机第一次启动Finder时,没有其他应用程序运行时,在64兆系统中wired-memory可以消耗大约14兆字节 在128兆系统中消耗17兆字节。

Wired memory pages are not immediately moved back to the free list when they become invalid. Instead they are “garbage collected” when the free-page count falls below the threshold that triggers page out events.
Wired memory页在无效时不会立即移回空闲列表。相反,当空闲页面数低于触发page out事件的阈值时,它们会被“垃圾收集”。

Page Lists in the Kernel

The kernel maintains and queries three system-wide lists of physical memory pages:

  • 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 removed 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.
  • 活动列表包含当前映射到内存和最近访问的页面。
  • 非活动列表包含当前驻留在物理内存中但最近未被访问的页面。这些页面包含有效的数据,但是可以随时从内存中删除。
  • 空闲列表包含 没有和任何VM对象关联的物理内存页。任何需要这些页面的进程都可以立即使用这些页面。

When the number of pages on the free list falls below a threshold (determined by the size of physical memory), the pager attempts to balance the queues. It does this by pulling pages from the inactive list. If a page has been accessed recently, it is reactivated and placed on the end of the active list. In OS X, if an inactive page contains data that has not been written to the backing store recently, its contents must be paged out to disk before it can be placed on the free list. (In iOS, modified but inactive pages must remain in memory and be cleaned up by the application that owns them.) If an inactive page has not been modified and is not permanently resident (wired), it is stolen (any current virtual mappings to it are destroyed) and added to the free list. Once the free list size exceeds the target threshold, the pager rests.
当空闲列表中的页面数低于阈值(由物理内存大小决定)时,pager将尝试平衡队列。它通过从非活动列表中提取页面来实现这一点。如果最近访问了某个页面,则重新激活该页面并将其放置在活动列表的末尾。在OS X中,如果一个不活动的页面包含了最近没有写到后台存储的数据,那么在它被放到空闲列表之前,它的内容必须被分页到磁盘上。(在iOS中,修改但不活跃的页面必须保留在内存中,并由拥有它们的应用程序进行清理。) 如果一个不活动的页面没有被修改,并且不是永久驻留(wired),那么它将被窃取(它的任何当前虚拟映射都将被销毁)并添加到空闲列表中。一旦空闲列表大小超过目标阈值,pager就会停止。

The kernel moves pages from the active list to the inactive list if they are not accessed; it moves pages from the inactive list to the active list on a soft fault (see Paging In Process). When virtual pages are swapped out, the associated physical pages are placed in the free list. Also, when processes explicitly free memory, the kernel moves the affected pages to the free list.
内核将未访问的页面从活动列表移动到非活动列表;通过soft fault将页面从非活动列表移动到活动列表 (请参阅分页In Process)。当虚拟页面交换出去时,相关的物理页面被放在空闲列表中。此外,当进程显式地释放内存时,内核将受影响的页面移动到空闲列表。

Paging Out Process

In OS X, when the number of pages in the free list dips below a computed 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:

  1. If a page in the active list is not recently touched, it is moved to the inactive list.
  2. If a page in the inactive list is not recently touched, the kernel finds the page’s VM object.
  3. If the VM object has never been paged before, the kernel calls an initialization routine that creates and assigns a default pager object.
  4. The VM object’s default pager attempts to write the page out to the backing store.
  5. 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.

在OS X中,当空闲列表中的页面数量低于计算的阈值时,内核通过将不活动的页面从内存中交换出来,从而回收空闲列表的物理页面。为此,内核遍历活动和非活动列表中的所有驻留页面,执行以下步骤:

  1. 如果活动列表中的某个页面最近未被触摸,它将被移到非活动列表。
  2. 如果非活动列表中的一个页面最近没有被使用,内核将找到页面的VM对象。
  3. 如果VM对象以前从未被分页,那么内核将调用一个初始化例程,该例程创建并分配一个默认的分页器对象。
  4. VM对象的默认分页器尝试将页面写到后台存储。
  5. 如果pager成功,内核释放页面占用的物理内存,并将页面从非活动列表移动到空闲列表。

Paging In Process

The final phase of virtual memory management moves pages into physical memory, either from the backing store or from the file containing the page data. A memory access fault initiates the page-in process. A memory access fault occurs when code tries to access data at a virtual address that is not mapped to physical memory. There are two kinds of faults:

  1. A soft fault occurs when the page of the referenced address is resident in physical memory but is currently not mapped into the address space of this process.
  2. A hard fault occurs when the page of the referenced address is not in physical memory but is swapped out to backing store (or is available from a mapped file). This is what is typically known as a page fault.

  3. soft fault:当引用地址的页驻留在物理内存中,但当前没有映射到此进程的地址空间时,就会发生此错误。

  4. hard fault:当引用地址的页面不在物理内存中,而是被交换到后备存储器(或从映射文件中可用)时,就会出现。这就是通常所说的页面错误。

When any type of fault occurs, the kernel locates the map entry and VM object for the accessed region. The kernel then goes through the VM object’s list of resident pages. If the desired page is in the list of resident pages, the kernel generates a soft fault. If the page is not in the list of resident pages, it generates a hard fault.

For soft faults, the kernel maps the physical memory containing the pages to the virtual address space of the process. The kernel then marks the specific page as active. If the fault involved a write operation, the page is also marked as modified so that it will be written to backing store if it needs to be freed later.

For hard faults, the VM object’s pager finds the page in the backing store or from the file on disk, depending on the type of pager. After making the appropriate adjustments to the map information, the pager moves the page into physical memory and places the page on the active list. As with a soft fault, if the fault involved a write operation, the page is marked as modified.


Mac的内存使用:Wired, Active, Inactive和Free

  • Wired(联动): 系统核心占用的,永远不会从系统物【[内存】中去除。
  • Active(活跃): 表示这些内存数据正在使用种,或者刚被使用过。
  • Inactive(非活跃): 表示这些内存中的数据是有效的,但是最近没有被使用。
  • Free(可用空间): 表示这些内存中的数据是无效的,即内存剩余量!


  • 首先,如果Inactive的数据最近被调用了,系统会把它们的状态改变成Active,并且在原有Active内存逻辑地址的后面;
  • 其次,如果Inactive的内存数据最近没有被使用过,但是曾经被更改过,而还没有在硬盘的相应虚拟[内存]中做修改,系统会对相应硬盘的虚拟内存做修改,并把这部分物理内存释放为free供程序使用。
  • 再次,如果inactive[内存]中得数据被在映射到硬盘后再没有被更改过,则直接释放成free。
  • 最后如果active的内存一段时间没有被使用,会被暂时改变状态为inactive。

  所以,如果你的系统里有少量的free memeory和大量的inactive的memeory,说明你的内存是够用的,系统运行在最佳状态,只要需要,系统就会使用它们,不用担心。

如果系统的free memory和inactive memory都很少,而active memory很多,说明你的[内存]不够了。当然一开机,大部分[内存]都是free,这时系统反而不在最佳状态,因为很多数据都需要从硬盘调用,速度反而慢了。

Free memory

This is RAM that’s not being used.

Wired memory

Information in this memory can’t be moved to the hard disk, so it must stay in RAM. The amount of Wired memory depends on the applications you are using.

Active memory

This information is currently in memory, and has been recently used.

Inactive memory

This information in memory is not actively being used, but was recently used.

For example, if you’ve been using Mail and then quit it, the RAM that Mail was using is marked as Inactive memory. This Inactive memory is available for use by another application, just like Free memory. However, if you open Mail before its Inactive memory is used by a different application, Mail will open quicker because its Inactive memory is converted to Active memory, instead of loading Mail from the slower hard disk.


This is the total amount of memory used.

VM size

This is the total amount of Virtual Memory for all processes on your Mac.

Page ins / Page outs

This refers to the amount of information moved between RAM and the hard disk. This number is a cumulative amount of data that Mac OS X has moved between RAM and disk space.

Tip: Page outs occur when your Mac has to write information from RAM to the hard drive (because RAM is full). Adding more RAM may reduce page outs.

Swap used

This is the amount of information copied to the swap file on your hard drive.