系统与基础设施duartes.org编辑精选
程序在内存中的解剖
Anatomy of a Program in Memory
2009 — 2016,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这篇经典文章图文并茂地剖析了一个程序在内存中的真实布局:虚拟地址空间如何划分(栈、堆、数据段、内核空间),以及 Linux/x86 分页机制下虚拟内存到物理内存的映射过程。作者 Gustavo Duarte 的博客以深入浅出的底层技术讲解著称。
为什么经久不衰:它把虚拟内存、分页和进程地址空间这些每个程序员都该懂却常被含糊带过的基础,讲得直观透彻,多年后仍是入门底层原理的最佳读物之一。
HN 评论精选
强烈推荐所有系统程序员定期重读此文。
完全值回票价。每个 Linux 开发者,尤其是 C/C++ 程序员,都应该把这篇文章列为必读——每年读一遍。
lbrandy查看英文原文
Totally worth the price of admission. Every linux dev, especially c/c++ programmers, should be required to read this post. Once a year.
补充:内核占用地址空间是性能取舍,而非硬性要求,不同系统有不同设计。
为内核保留一部分地址空间其实是一种性能优化,并非必需。在 32 位 macOS 中,内核拥有和普通进程一样独立的地址空间,系统调用时需要拷贝或映射数据。好处是用户进程能用满全部 4GB,代价是额外开销和 TLB 刷新。32/64 位 iOS 和 64 位 macOS 则采用把内核地址空间映射进所有进程的通行做法,尤其在 64 位下没有理由不这样做。
xenadu02查看英文原文
> Once virtual addresses are enabled, they apply to all software running in the machine, including the kernel itself. Thus a portion of the virtual address space must be reserved to the kernel Reserving a portion of the address space for the kernel is a performance optimization and not necessarily required. In 32-bit macOS the kernel has its own separate address space just like a process. Syscalls copy or map data in and out. The benefit is user mode processes can use all 4 GB. The obvious downside is the extra overhead and TLB flushes. 32/64-bit iOS and 64-bit macOS use the standard convention if having the kernel's address space mapped into all processes. Especially on 64-bit there is no benefit to doing otherwise.
推荐一个可视化二进制内存布局的实用工具。
非常好的文章和博客。除了文中提到的 nm 和 objdump,还可以用我新写的工具 Bloaty McBloatface,它的 -v 选项能同时输出文件域和虚拟地址域的内存映射图。
haberman查看英文原文
Really great article and blog. > You can examine binary images using the nm and objdump commands to display symbols, their addresses, segments, and so on. You can also use my new tool Bloaty McBloatface (https://github.com/google/bloaty). Check out the -v option especially, which will dump a memory map of both the file domain and the VM address domain: $ ./bloaty `which ls` -v -d segments FILE MAP: [0, 19d44] LOAD [RX], LOAD [RX] [19d44, 19df0] [None], [Unmapped] [19df0, 1a5f4] LOAD [RW], LOAD [RW] [1a5f4, 1a700] [None], [Unmapped] [1a700, 1ae00] [None], [ELF Headers] VM MAP: [0, 400000] NO ENTRY [400000, 419d44] LOAD [RX], LOAD [RX] [419d44, 619df0] NO ENTRY [619df0, 61a5f4] LOAD [RW], LOAD [RW] [61a5f4, 61b360] LOAD [RW], LOAD [RW] VM SIZE FILE SIZE -------------- -------------- 95.1% 103Ki LOAD [RX] 103Ki 96.1% 4.9% 5.36Ki LOAD [RW] 2.00Ki 1.9% 0.0% 0 [ELF Headers] 1.75Ki 1.6% 0.0% 0 [Unmapped] 440 0.4% 100.0% 108Ki TOTAL 107Ki 100.0% If you leave off "-d segments" the map will include all sections too (like .bss, .text, etc). Here is an example of that output: http://pastebin.com/3XGcqA8k
文章虽好,但不少读者的评论暴露了对虚拟内存基础的误解。
从文章评论来看,很多人还是没搞懂虚拟内存的基础,比如有人惊讶 Windows 内核占用内存是 Linux 的两倍,有人疑惑内核空间为什么要占 1GB。
yan查看英文原文
According to the comments on that article, the people still don't understand the basics of virtual memory. "Great article! / Quite shocked to know that windows takes double the kernel memory compared to Linux." "I’m wondering, though, why does the kernel space consume 1gb? That seems like a lot.."
历次讨论
同类还值得读
TTY 揭秘(2008)
The TTY demystified (2008)
经典文章深入剖析了 Unix/Linux 中 TTY 子系统的工作原理,包括线路规程、终端驱动、信号与控制字符等机制。
系统与基础设施上首页 11 次 · 2009–2025linusakesson.net编辑精选为什么 GNU grep 这么快
Why GNU grep is Fast
FreeBSD 邮件列表上一篇广为流传的帖子,解释 GNU grep 为什么快:核心在于 Boyer-Moore 字符串搜索算法配合精心优化的内部循环和 I/O,让程序在搜索时“…
系统与基础设施上首页 9 次 · 2010–2022lists.freebsd.orgXplain:为普通人讲解 X11
Xplain – Explaining X11 for the rest of us
《Xplain》是作者(HN 用户 Jasper_)写的一系列 X11 长文,用他在浏览器中重建的 X11 绘图模型做交互演示,讲解 X11 的绘图模型与窗口系统设计。
系统与基础设施上首页 6 次 · 2013–2020magcius.github.io编辑精选旋风教程:为 Linux 创建极小 ELF 可执行文件(1999)
A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux (1999)
breadbox 写于 1999 年的经典教程,主题是如何为 Linux 手工构造并裁剪出体积极小的 ELF 可执行文件。
系统与基础设施上首页 6 次 · 2007–2025muppetlabs.comPostgreSQL 内部原理
The internals of PostgreSQL
《The Internals of PostgreSQL》是一本免费在线书籍,系统讲解 PostgreSQL 的内部实现,包括查询处理、并发控制、WAL、缓冲区管理等核心机制。
系统与基础设施上首页 5 次 · 2017–2025interdb.jp编辑精选