Lobsters | 📄 原文链接 | 2026-10-24 收录

用私有映射白拿一份 CoW 内存副本

来源:sebsite.pw — 2026-07-28

📋 概述

作者发现一个利用写时复制来做内存复制的技巧:先 memfd_create 建一个内存文件并 mmap 成共享映射,往里面写模板数据;之后用 MAP_PRIVATE 再映射同一个 fd,就得到一份「副本」,此刻不占任何物理内存。改动副本的某一页时才真正分配一页 4KiB,其余页继续与模板共享。实测分配 32MiB 再复制一份,物理内存总用量只有 32.004MiB。

🔑 核心要点

💡 金句

this is cool because regardless of the size of the initial allocation, the copy isn't taking up any physical memory.
← 返回 Lobsters 首页