查看虚拟内存

文章目录
  1. 1. Viewing Virtual Memory Statistics(查看虚拟内存统计信息)
  2. 2. Viewing Mach-O Code Pages
  3. 3. Viewing Virtual Memory Regions

If you need more detailed information about virtual memory usage, you can use the top, vm_stat, pagestuff, and vmmap command-line tools for analyzing your Mac apps. The information returned by these tools ranges from summary information about all the system processes to detailed information about a specific process.
如果您需要关于虚拟内存使用的更详细的信息,您可以使用上面的vm_stat、pagestuff和vmmap命令行工具来分析您的Mac应用程序。这些工具返回的信息范围从关于所有系统流程的总结信息到关于特定流程的详细信息。

The following sections provide information on using the vm_stat, pagestuff, and vmmap tools to gather detailed memory information. For more information on using Instruments to analyze memory, see Instruments User Guide and the other articles in this document. For information on how to use the top tool,
以下部分提供了关于使用vm_stat、pagestuff和vmmap工具收集详细内存信息的信息。有关使用仪器分析内存的更多信息,请参阅仪器用户指南和本文中的其他文章。有关如何使用顶部工具的信息,请参见2。see Performance Overview.

Viewing Virtual Memory Statistics(查看虚拟内存统计信息)

The vm_stat tool displays high-level statistics about the current virtual memory usage of the system. By default, vm_stat displays these statistics once, but you can specify an interval value (in seconds) to update these statistics continuously. For information on the usage of this tool, see the vm_stat man page.
vm_stat工具显示关于系统当前虚拟内存使用情况的高级统计信息。默认情况下,vm_stat只显示这些统计信息一次,但是您可以指定一个间隔值(以秒为单位)来持续更新这些统计信息。有关此工具使用的信息,请参见vm_stat手册页。

Listing 1 shows an example of the output from vm_stat.

1
2
3
4
5
6
7
8
9
10
11
12
Mach Virtual Memory Statistics: (page size of 4096 bytes)
Pages free: 3194.
Pages active: 34594.
Pages inactive: 17870.
Pages wired down: 9878.
"Translation faults": 6333197.
Pages copy-on-write: 81385.
Pages zero filled: 3180051.
Pages reactivated: 343961.
Pageins: 33043.
Pageouts: 78496.
Object cache: 66227 hits of 96952 lookups (68% hit rate)

Viewing Mach-O Code Pages

The pagestufftool displays information about the specified logical pages of a file conforming to the Mach-O executable format. For each specified page of code, symbols (function and static data structure names) are displayed. All pages in the TEXT, text section are displayed if no page numbers are given.
pagestufftool显示符合Mach-O可执行格式的文件的指定逻辑页的信息。对于每个指定的代码页,将显示符号(函数和静态数据结构名称)。如果没有给出页码,则显示__TEXT节中的所有页面。

Listing 2 shows part of the output from pagestuff for the TextEdit application. This output is the result of running the tool with the -a option, which prints information about all of the executable’s code pages. It includes the virtual address locations of each page and the type of information on that page.
清单2显示了TextEdit应用程序的页面内容的部分输出。此输出是使用-a选项运行工具的结果,该选项打印关于可执行文件的所有代码页的信息。它包括每个页面的虚拟地址位置和页面上的信息类型。

Listing 2 Partial output of pagestuff tool

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
File Page 0 contains Mach-O headers
File Page 1 contains Mach-O headers
File Page 2 contains contents of section (__TEXT,__text)
Symbols on file page 2 virtual address 0x3a08 to 0x4000
File Page 3 contains contents of section (__TEXT,__text)
Symbols on file page 3 virtual address 0x4000 to 0x5000
File Page 4 contains contents of section (__TEXT,__text)
Symbols on file page 4 virtual address 0x5000 to 0x6000

...
File Page 22 contains contents of section (__TEXT,__cstring)
File Page 22 contains contents of section (__TEXT,__literal4)
File Page 22 contains contents of section (__TEXT,__literal8)
File Page 22 contains contents of section (__TEXT,__const)
Symbols on file page 22 virtual address 0x17000 to 0x17ffc
File Page 23 contains contents of section (__DATA,__data)
File Page 23 contains contents of section (__DATA,__la_symbol_ptr)
File Page 23 contains contents of section (__DATA,__nl_symbol_ptr)
File Page 23 contains contents of section (__DATA,__dyld)
File Page 23 contains contents of section (__DATA,__cfstring)
File Page 23 contains contents of section (__DATA,__bss)
File Page 23 contains contents of section (__DATA,__common)
Symbols on file page 23 virtual address 0x18000 to 0x18d48
0x00018000 _NXArgc
0x00018004 _NXArgv
0x00018008 _environ
0x0001800c ___progname
...

In the preceding listing, if a page exports any symbols, those symbols are also displayed by the -a option. If you want to view the symbols for a single page, pass in the desired page number instead of the -a option. For more information about the pagestuff tool and its supported options, see the pagestuff man page.

在前面的列表中,如果一个页面导出任何符号,那么这些符号也会被a选项显示。如果要查看单个页面的符号,请输入所需的页码,而不是-a选项。有关pagestuff工具及其支持选项的更多信息,请参见pagestuff man页面。

Viewing Virtual Memory Regions

The vmmap and vmmap64 tools display the virtual memory regions allocated for a specified process. These tools provide access to the virtual memory of 32-bit and 64-bit applications, respectively. You can use them to understand the purpose of memory at a given address and how that memory is being used. For each virtual-memory region, these tools display the type of page, the starting address, region size (in kilobytes), read/write permissions, sharing mode, and the purpose of the pages in that region.
vmmap和vmmap64工具显示为指定进程分配的虚拟内存区域。这些工具分别提供对32位和64位应用程序的虚拟内存的访问。您可以使用它们来理解给定地址的内存用途以及内存的使用方式。对于每个虚拟内存区域,这些工具显示页面的类型、起始地址、区域大小(单位为千字节)、读/写权限、共享模式和该区域的页面用途。

The following sections show you how to interpret the output from the vmmap tool. For more information about the vmmap and vmmap64 tools, see the vmmap or vmmap64 man pages.
以下部分将向您展示如何解释vmmap工具的输出。有关vmmap和vmmap64工具的更多信息,请参见vmmap或vmmap64手册页面。

Sample Output From vmmap
Listing 3 shows some sample output from the vmmap tool. This example is not a full listing of the tool’s output but is an abbreviated version showing the primary sections.

Listing 3 Typical output of vmmap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
==== Non-writable regions for process 313
__PAGEZERO 0 [ 4K] ---/--- SM=NUL ...ts/MacOS/Clock
__TEXT 1000 [ 40K] r-x/rwx SM=COW ...ts/MacOS/Clock
__LINKEDIT e000 [ 4K] r--/rwx SM=COW ...ts/w/Clock
90000 [ 4K] r--/r-- SM=SHM
340000 [3228K] r--/rwx SM=COW 00000100 00320...
789000 [3228K] r--/rwx SM=COW 00000100 00320...
Submap 90000000-9fffffff r--/r-- machine-wide submap
__TEXT 90000000 [ 932K] r-x/r-x SM=COW /usr/lib/libSystem.B.dylib
__LINKEDIT 900e9000 [ 260K] r--/r-- SM=COW /usr/lib/libSystem.B.dylib
__TEXT 90130000 [ 740K] r-x/r-x SM=COW .../Versions/A/CoreFoundation
__LINKEDIT 901e9000 [ 188K] r--/r-- SM=COW .../Versions/A/CoreFoundation
__TEXT 90220000 [2144K] r-x/r-x SM=COW .../Versions/A/CarbonCore
__LINKEDIT 90438000 [ 296K] r--/r-- SM=COW .../Versions/A/CarbonCore

[...data omitted...]

==== Writable regions for process 606
__DATA 18000 [ 4K] rw-/rwx SM=PRV /Contents/MacOS/TextEdit
__OBJC 19000 [ 8K] rw-/rwx SM=COW /Contents/MacOS/TextEdit
MALLOC_OTHER 1d000 [ 256K] rw-/rwx SM=PRV
MALLOC_USED(DefaultMallocZone_0x5d2c0) 5d000 [ 256K] rw-/rwx SM=PRV
9d000 [ 372K] rw-/rwx SM=COW 33320000 00000020 00000000 00001b84...
VALLOC_USED(DefaultMallocZone_0x5d2c0) ff000 [ 36K] rw-/rwx SM=PRV
MALLOC_USED(CoreGraphicsDefaultZone_0x10 108000 [ 256K] rw-/rwx SM=PRV
MALLOC_USED(CoreGraphicsRegionZone_0x148 148000 [ 256K] rw-/rwx SM=PRV

[...data omitted...]

Submap a000b000-a012ffff r--/r-- process-only submap
__DATA a0130000 [ 28K] rw-/rw- SM=COW .../Versions/A/CoreFoundation
Submap a0137000-a021ffff r--/r-- process-only submap
__DATA a0220000 [ 20K] rw-/rw- SM=COW .../Versions/A/CarbonCore
Submap a0225000-a048ffff r--/r-- process-only submap
__DATA a0490000 [ 12K] rw-/rw- SM=COW .../IOKit.framework/Versions/A/IOKit
Submap a0493000-a050ffff r--/r-- process-only submap
__DATA a0510000 [ 36K] rw-/rw- SM=COW .../Versions/A/OSServices
b959e000 [ 4K] rw-/rw- SM=SHM
b95a0000 [ 4K] rw-/rw- SM=SHM
b9630000 [ 164K] rw-/rw- SM=SHM
b965a000 [ 896K] rw-/rw- SM=SHM
bff80000 [ 504K] rw-/rwx SM=ZER
STACK[0] bfffe000 [ 4K] rw-/rwx SM=PRV
bffff000 [ 4K] rw-/rwx SM=PRV
__DATA c000c000 [ 4K] rw-/rwx SM=PRV .../Versions/A/ApplicationEnhancer
STACK[1] f0001000 [ 512K] rw-/rwx SM=PRV
ff002000 [12272K] rw-/rw- SM=SHM

==== Legend
SM=sharing mode:
COW=copy_on_write PRV=private NUL=empty ALI=aliased
SHM=shared ZER=zero_filled S/A=shared_alias

==== Summary for process 313
ReadOnly portion of Libraries: Total=27420KB resident=12416KB(45%) swapped_out_or_unallocated=15004KB(55%)
Writable regions: Total=21632KB written=536KB(2%) resident=1916KB(9%) swapped_out=0KB(0%) unallocated=19716KB(91%)

If you specify the -d parameter (plus an interval in seconds), vmmap takes two snapshots of virtual-memory usage—one at the beginning of a specified interval and the other at the end—and displays the differences. It shows three sets of differences:

  • individual differences
  • regions in the first snapshot that are not in the second
  • regions in the second snapshot that are not in the first

如果您指定-d参数(加上以秒为单位的间隔),vmmap将获取两个虚拟内存使用快照(一个在指定的间隔开始,另一个在结束),并显示差异。它显示了三组差异:

  • 个体差异
  • 第一个快照中的区域而不是第二个快照中的区域
  • 第二个快照中不属于第一个快照的区域

Interpreting vmmap’s Output
The columns of vmmap output have no headings. Instead you can interpret the type of data in each column by its format. Table 1 describes these columns.

Table 1 Column descriptions for vmmap

Table 1 Column descriptions for vmmap

Column Number Example Description
1 __TEXT, __LINKEDIT, MALLOC_USED, STACK, and so on The purpose of the memory. This column can contain the name of a Mach-O segment or the memory allocation technique.
2 (DefaultMallocZone_0x5d2c0) If present, the zone used for allocation.
3 4eee000 The virtual memory address of the region.
4 [ 124K] The size of the region, measured in kilobytes
5 rw-/rwx Read, write and execution permissions for the region. The first set of flags specifies the current protection for the region. The second set of values specifies the maximum protection for the region. If an entry contains a dash (-), the process does not have the target permission.
6 SM=PRV Sharing mode for the region, either COW (copy-on-write), PRV (private), NUL (empty), ALI (aliased), or SHM (shared).
7 ...ts/MacOS/Clock The end of the pathname identifying the executable mapped into this region of virtual memory. If the region is stack or heap memory, nothing is displayed in this column.

Column 1 identifies the purpose of the memory. A TEXT segment contains read-only code and data. A DATA segment contains data that may be both readable and writable. For allocated data, this column shows how the memory was allocated, such as on the stack, using malloc, and so on. For regions loaded from a library, the far right column shows the name of the library loaded into memory.
第1列标识内存的用途。TEXT段包含只读代码和数据。DATA段包含可读和可写的数据。对于已分配的数据,这一列显示如何分配内存,例如使用malloc在堆栈上,等等。对于从库中加载的区域,最右边的列显示加载到内存中的库的名称。

The size of the virtual memory region (column 4) represents the total size reserved for that region. This number may not reflect the actual number of memory pages allocated for the region. For example, calling vm_allocate reserves a set of memory pages but does not allocate any physical memory until the pages are actually touched. Similarly, a memory-mapped file may reserve a set of pages, but the system does not load pages until a read or write event occurs on the file.
虚拟内存区域的大小(第4列)表示该区域保留的总大小。这个数字可能不能反映为该区域分配的内存页的实际数量。例如,调用vm_allocation会保留一组内存页,但在实际操作这些页之前不会分配任何物理内存。类似地,内存映射文件可以保留一组页面,但是系统在文件上发生读或写事件之前不会加载页面。

The protection mode (column 5) describes the access restrictions for the memory region. A memory region contains separate flags for read, write, and execution permissions. Each virtual memory region has a current permission, and a maximum permission. In the output from vmmap, the current permission appears first followed by the maximum permission. Thus, if the permissions are “r–/rwx“ the page is currently read-only but allows read, write, and execution access as its maximum allowed permissions. Typically, the current permissions do not permit writing to a region. However, these permissions may change under certain circumstances. For example, a debugger may request write access to a page in order to set a breakpoint.
保护模式(第5列)描述了内存区域的访问限制。内存区域包含用于读取、写入和执行权限的单独标志。每个虚拟内存区域都有一个当前权限和最大权限。在vmmap的输出中,当前权限首先显示为最大权限。因此,如果权限是“r- /rwx”,页面当前是只读的,但允许读、写和执行访问作为其最大允许的权限。通常,当前权限不允许写入区域。但是,这些权限在某些情况下可能会改变。例如,调试器可以请求对页面的写访问,以便设置断点。

The sharing mode (SM= field) tells you whether pages are shared between processes and what happens when pages are modified. Private pages (PRV) are visible only to the process and are allocated as they are used. Private pages can also be paged out to disk. Copy-on-write (COW) pages are shared by multiple processes (or shared by a single process in multiple locations). When the page is modified, the writing process then receives its own copy of the page. Empty (NUL) sharing implies that the page does not really exist in physical memory. Aliased (ALI) and shared (SHM) memory are shared between processes.
共享模式(SM= field)告诉您进程之间是否共享页面,以及修改页面时会发生什么。私有页面(PRV)只对进程可见,并在使用它们时进行分配。还可以将私有页面分页到磁盘。写时复制(COW)页面由多个进程共享(或由多个位置的单个进程共享)。当页面被修改时,编写过程将收到它自己的页面副本。空(NUL)共享意味着页面并不真正存在于物理内存中。别名(ALI)和共享(SHM)内存在进程之间共享。

The sharing mode typically describes the general mode controlling the region. For example, as copy-on-write pages are modified, they become private to the application. However, the region containing those private pages is still copy-on-write until all pages become private. Once all pages are private, the sharing mode changes to private.
共享模式通常描述控制该区域的通用模式。例如,当对写后复制的页面进行修改时,它们将成为应用程序的私有页面。但是,包含这些私有页面的区域仍然是“写时复制”,直到所有页面变为私有。一旦所有页面都是私有的,共享模式就会变为私有。

Some lines in the output of vmmap describe submaps. A submap is a shared set of virtual memory page descriptions that the operating system can reuse between multiple processes. For example, the memory between 0x90000000 and 0xAFFFFFFF is a submap containing the most common dynamic libraries. Submaps minimize the operating system’s memory usage by representing the virtual memory regions only once. Submaps can either be shared by all processes (machine-wide) or be local to the process (process-only). If the contents of a machine-wide submap are changed—for example, the debugger makes a section of memory for a dynamic library writable so it can insert debugging traps—then the submap becomes local, and the kernel allocates memory to store the extra copy.
vmmap输出中的一些行描述子映射。子映射是操作系统可以在多个进程之间重用的一组共享的虚拟内存页面描述。例如,0x90000000和0xAFFFFFFF之间的内存是包含最常见动态库的子映射。子映射只表示一次虚拟内存区域,从而最小化操作系统的内存使用。子映射可以由所有进程(机器范围内)共享,也可以是进程(仅处理)的本地映射。如果修改了机器范围的子映射的内容(例如,调试器为动态库编写一个内存段,以便它可以插入调试trap),那么子映射就变成本地的,内核分配内存来存储额外的副本。

[a]:

[a]: