你的计算机内存是个谎言Your Computer Memory is a Lie


AI free token/AI免费积分
你的计算机内存是个谎言

当你刚开始学习编程时,内存看起来相当直白。你有一堆字节,每个字节都有一个地址。如果你想读取或写入某些内容,只需指定地址,计算机就会去那个位置并按你的意愿执行操作。当然,有些地址可能被硬件或操作系统保留,但整体概念很简单:内存就是一个巨大的字节数组。

正如我们把导线当作理想导体一样,那种“一大片内存数组”的模型过去也只是对事实的轻微扭曲。在现代计算机上,这几乎完全是谎言。你使用的地址很可能不是物理地址。你读取的数据可能根本不是来自 RAM。即使确实来自 RAM,它也可能正位于多级缓存中的某一级。如果你顺着这个兔子洞追根究底,你会发现 SSD 可以说只是存储器层次结构中的另一层。

处理器的根本问题在于,其速度已经远远超过内存。现代处理器每个时钟周期可以执行多条指令,但访问普通 DRAM 可能需要数百个周期。如果处理器每次需要数据时都必须等待 RAM,那么其强大的算力就会大量浪费。

解决方案是缓存。将常用数据的副本保存在更靠近处理器、更小更快的存储器中。大多数现代桌面处理器至少有三层缓存。L1 缓存容量很小但速度极快,通常还分为指令缓存和数据缓存。L2 更大一些、速度稍慢,而 L3 则更大,通常也更慢。

如今多核心已经很常见,情况也因此变得更有意思。通常,每个核心都有自己的 L1 和 L2 缓存,而多个核心共享一个 L3 缓存。但这并非普遍情况。有些处理器在核心之间共享 L2 缓存。另一些处理器则拥有多个 L3 缓存,每个缓存服务一组核心。具体安排取决于处理器架构。

一个重要细节是:缓存通常不是按字节工作的。它们以称为“缓存行”的块进行传输和跟踪,在现代桌面处理器上通常为 64 字节。如果你读取一个未被缓存的字节,处理器通常会取回包含该字节的整个缓存行。

这就是为什么顺序内存访问通常比随机访问快得多。如果你读取下一个字节,它很可能已经在缓存中。硬件预取器甚至可能预测你接下来需要什么。

但也可能出现问题。想象两个核心正在修改完全不同的变量。如果这些变量恰好占用同一条缓存行,核心之间可能会反复使对方的缓存副本失效,即使它们在逻辑上并没有共享数据,也会浪费时间。

诸如 MESI 这样的协议可保持缓存一致性,协调哪个核心拥有某条缓存行以及哪些副本有效。MESI 是缓存行状态的缩写:已修改、独占、共享或无效。内存系统必须弄清楚如何让缓存保持同步。但一致性并不能消除多线程程序中对同步或内存排序规则的需求。关键在于,即使是我们这种简单的按字节寻址的内存,实际上也在以更大的块悄悄运作。

到目前为止,我们一直假设你提供的地址标识的是 RAM 中的一个位置。通常并非如此。现代操作系统使用虚拟内存。你的程序会看到一个虚拟地址空间,由操作系统和处理器协同将其映射到物理内存。

内存管理单元,即 MMU,通常使用页表将虚拟地址转换为物理地址。这使得不同的程序可以使用相同的虚拟地址而互不干扰。它还能提供内存保护,并允许共享物理页。当然,地址转换需要耗费时间。因此,我们引入了另一层缓存。

转换后备缓冲区,即 TLB,会记录最近使用过的地址转换。TLB 命中可以避免可能代价高昂的页表遍历。某些处理器甚至采用多级 TLB。与通常以缓存行为单位的普通 CPU 缓存不同,虚拟内存通常以页为单位运作,常见大小为 4 KB,同时也支持更大的页。

注意正在发生什么。我们有数据缓存、指令缓存,以及用于定位数据和指令所需信息的缓存。读取一个变量这样简单的行为,已经变得出人意料地复杂。

即使我们穿过缓存并最终到达 RAM,事情也未必简单。在多处理器或某些多核配置的系统上,内存访问可能是非一致性的。这被称为 NUMA,即 Non-Uniform Memory Access。

处理器可以相对快速地访问连接到其自身 NUMA 节点的内存,而访问与另一个节点关联的内存则需要跨过一个互连。这两个位置都是普通的 RAM,也都可能出现在同一地址空间中,但访问时间和可用带宽可能不同。

操作系统会尽量将内存放置在正在使用它的处理器附近。因此,将线程迁移到另一个 CPU 可能会改变其内存性能,即使程序中的地址没有发生变化。

而且,NUMA 并不局限于传统多处理器服务器。采用小芯片、异构内存以及 CXL 等技术的现代系统,正让内存拓扑变得越来越有趣。

虚拟内存还有另一个技巧。虚拟页在你访问它的时刻,并不一定非得由物理 RAM 提供后备存储。

当程序访问的页面当前没有以可用方式映射时,处理器会产生缺页中断。操作系统决定如何处理。有时它只是分配一个新页面。有时它会从可执行文件或内存映射文件中检索数据。其他时候,该页面已从 RAM 移出到交换空间,必须重新取回。需要注意的是,可执行数据等内容通常是不可变的,因此大多数操作系统不会将它们换出。它们只是直接丢弃这些内容,因为知道需要时可以稍后重新加载相同的页面。

这意味着你的程序可以寻址当前不在物理内存中的数据。当然,访问 SSD 远比访问 RAM 慢得多,因此过度的交换操作会让计算机陷入困境。

然而,虚拟内存实际上并不需要交换。即使在永远不会将页面移动到磁盘的系统上,地址转换和保护仍然有用。

假设你的程序读取一个文件。你可能认为操作系统会向 SSD 发送请求并等待数据。但如果该文件最近被访问过,数据可能已经存在于 RAM 中。

操作系统会维护一个文件系统页面缓存,将频繁或最近访问过的文件数据保留在内存中。写入操作也可能先在 RAM 中进行缓冲,然后再最终写入持久化存储。

这使得 RAM 充当了二级存储的缓存,类似于 L3 充当 RAM 的缓存。

这个类比并不完美。RAM 也是运行程序的主要工作存储,而匿名内存不一定是磁盘上任何内容的副本。但整体的层次结构是清晰的:寄存器、CPU 缓存、RAM 和持久化存储在速度、容量和成本之间提供逐级不同的权衡。

本文来源:https://hackaday.com/2026/10/12/your-computer-memory-is-a-lie/
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。

Just like we pretend that a wire is a perfect conductor, the big array of memory model used to be a slight twisting of the truth. On a modern computer, however, it is almost entirely a lie. The address you use probably isn’t a physical address. The data you read might not come from RAM at all. Even if it does, it might be sitting in one of several caches. And if you follow the rabbit hole far enough, you discover that your SSD is arguably just another level in the memory hierarchy.

The fundamental problem is that processors have gotten much faster than memory. A modern processor can execute several instructions per clock cycle, but accessing ordinary DRAM can take hundreds of cycles. If the processor had to wait for RAM every time it needed data, much of that impressive computing power would go to waste.

The solution is caching. Keep copies of frequently used data in smaller, faster memories closer to the processor. Most modern desktop processors have at least three levels of cache. The L1 cache is tiny but extremely fast, and often split between instructions and data. L2 is larger and somewhat slower, while L3 is even larger and usually slower as well.

Things get interesting with multiple cores, as is often the case these days. Typically, each core has its own L1 and L2 caches, while several cores share an L3 cache. But that’s not universal. Some processors share L2 caches between cores. Others have multiple L3 caches, each serving a group of cores. The exact arrangement depends on the processor architecture.

One important detail: caches don’t generally work byte by byte. They transfer and track blocks called cache lines, commonly 64 bytes on modern desktop processors. If you read one byte that isn’t cached, the processor generally fetches the entire line containing that byte.

This is why sequential memory access is often much faster than random access. If you read the next byte, there’s a good chance it’s already in the cache. Hardware prefetchers may even anticipate what you’ll need next.

But there can be problems. Imagine two cores modifying completely different variables. If those variables happen to occupy the same cache line, the cores may repeatedly invalidate each other’s copies, wasting time even though they aren’t logically sharing data.

Protocols such as MESI keep the caches coherent, coordinating which core owns a line and which copies are valid. MESI is an acronym for cache line states: modified, exclusive, shared, or invalid. Memory systems must figure out how to keep caches in sync. But coherence doesn’t eliminate the need for synchronization or memory-ordering rules in multithreaded programs. The point is that even our simple byte-addressed memory is secretly operating in larger chunks.

So far, we’ve assumed that the address you supply identifies a location in RAM. Usually, it doesn’t. Modern operating systems use virtual memory. Your program sees a virtual address space that the operating system and processor cooperate to map onto physical memory.

The memory management unit, or MMU, translates virtual addresses into physical addresses, typically using page tables. This lets different programs use identical virtual addresses without interfering with one another. It also provides memory protection and allows physical pages to be shared. Of course, translating addresses takes time. So we add another cache.

The translation lookaside buffer, or TLB, remembers recently used address translations. A TLB hit avoids a potentially expensive page-table walk. Some processors even have multiple levels of TLBs. Unlike the ordinary CPU cache, which generally operates on cache lines, virtual memory usually works with pages, often 4 KB, although larger pages are also supported.

Notice what’s happening. We have caches for data, caches for instructions, and caches for the information needed to locate the data and instructions. The simple act of reading a variable has become surprisingly complicated.

Even if we get through the caches and finally reach RAM, things aren’t necessarily simple. On systems with multiple processors or certain multicore arrangements, memory access can be non-uniform. This is called NUMA, or Non-Uniform Memory Access.

A processor may have relatively fast access to memory attached to its own NUMA node, while accessing memory associated with another node requires crossing an interconnect. Both locations are ordinary RAM, and both may appear in the same address space, but the access times and available bandwidth can differ.

The operating system tries to place memory near the processors using it. Moving a thread to another CPU can therefore change its memory performance, even though the program’s addresses haven’t changed.

And NUMA isn’t limited to traditional multiprocessor servers. Modern systems with chiplets, heterogeneous memory, and technologies such as CXL are making memory topology increasingly interesting.

Virtual memory has another trick. A virtual page doesn’t necessarily have to be backed by physical RAM at the moment you access it.

When a program accesses a page that isn’t currently mapped in a usable way, the processor generates a page fault. The operating system decides what to do. Sometimes it simply allocates a fresh page. Sometimes it retrieves data from an executable or memory-mapped file. Other times, the page was moved out of RAM to swap space and must be retrieved. Note that things like executable data are generally immutable, so most operating systems won’t swap them out. They simply trash them and know they can reload the same page later when needed.

This means your program can address data that isn’t presently in physical memory. Naturally, accessing an SSD is much slower than accessing RAM, so excessive swapping can bring a computer to its knees.

Virtual memory doesn’t actually require swapping, though. Address translation and protection are useful even on systems that never move pages to disk.

Your Computer Memory is a Lie

Suppose your program reads a file. You might imagine that the operating system sends a request to the SSD and waits for the data. But if the file was recently accessed, the data may already be in RAM.

Operating systems maintain a filesystem page cache, keeping frequently or recently accessed file data in memory. Writes may also be buffered in RAM before eventually reaching persistent storage.

This makes RAM act like a cache for secondary storage, much as L3 acts like a cache for RAM.

The analogy isn’t perfect. RAM is also the primary working storage for running programs, and anonymous memory isn’t necessarily a copy of anything on disk. But the general hierarchy is clear: registers, CPU caches, RAM, and persistent storage provide progressively different tradeoffs between speed, capacity, and cost.

Even SSDs have their own caches and address translations. Their flash translation layers map logical block addresses onto physical flash locations, hiding wear leveling and garbage collection from the host. Apparently, nobody wants to tell the truth about addresses.

But what if we could make RAM effectively larger without adding more? Compression offers another way to complicate our memory hierarchy. Instead of immediately moving an infrequently used page to disk, why not compress it and keep it in RAM?

Linux has two well-known mechanisms for doing this: zram and zswap. Zram creates a compressed block device in RAM. It’s commonly used as a swap device, allowing pages to be stored in compressed form rather than written to physical storage.

Zswap, on the other hand, is a compressed cache in front of a swap device. When Linux decides to swap out a page, zswap attempts to compress and retain it in RAM. If necessary, pages can eventually be evicted to the backing swap device.

Both trade processor time for memory capacity and reduced storage I/O. Decompressing a page costs CPU cycles, but that can be considerably cheaper than retrieving it from an SSD.

There’s a catch, though. These mechanisms operate through the swap subsystem. When a compressed page is needed again, it must be decompressed into an ordinary page before the program can access it. That means page faults, memory management overhead, and moving data around. But what if compressed memory could behave more like ordinary RAM?

That’s the idea behind CRAM, or Compressed RAM, a project presented by Meta’s Gregory Price at the October 2026 Linux Plumbers Conference.

Rather than treating compressed memory as a conventional swap device, CRAM proposes exposing it through a special NUMA node. Linux can then use existing memory-management mechanisms to move pages between ordinary DRAM and compressed memory. The most interesting part is the hardware-assisted compression and decompression, which allows access at much finer granularity than conventional page-oriented compressed swap.

The developers report impressive benchmark results, including read performance that approaches ordinary DRAM in some tests and dramatic improvements over zram for selected workloads.

That doesn’t mean compression has suddenly become free. Writes, compression ratios, hardware requirements, and managing incompressible data all matter. CRAM is still developmental work, not ready for everyday use.

But it illustrates where the memory hierarchy is heading. Instead of simply deciding whether a page is in RAM or on disk, the operating system may choose among ordinary RAM, compressed memory, slower memory tiers, and secondary storage.

Most programmers don’t need to think about any of this most of the time. That’s the point of an abstraction. But when performance matters, the illusion starts to leak. Sequential access can beat random access by enormous margins. Two threads modifying unrelated variables can fight over cache lines. Moving a process between CPUs can change its memory latency. A page that looks like ordinary memory might actually require decompression or a trip to an SSD.

On smaller embedded systems, of course, the picture may be very different. Without an MMU or elaborate caches, memory can look much more like the simple physical address space we learned about in the first place.

We started with an array of bytes. We ended up with a hierarchy of caches, translation tables, NUMA nodes, compressed memory pools, and persistent storage devices, all cooperating to maintain that simple illusion. Amazing how complicated something so simple can be.

暂无评论,快来发表第一条评论吧!No comments yet. Be the first!

📮 需求咨询📮 Inquiry