TL;DR
Copy-on-write, often shortened to COW, is a way to share data cheaply and still keep each owner's view correct. Several owners point at one copy, and nothing is duplicated while everyone only reads. The first time one owner tries to change the data, the system makes a private copy for that owner, and the others keep the original. The copy is delayed until it is needed, so a large block that is shared but rarely written costs almost nothing to share.
Builds on: Virtual Memory, because the copy is made by changing which frame a page points to, and Mutable State, because a write to a shared value is the reason a private copy is needed · Leads to: PagedAttention, which shares attention cache blocks between sampled outputs, and Layered File System
Where it's used
Creating processes. In Unix-style operating systems, fork is the system call that creates a child process as a copy of the caller. A child often overwrites only a small part of memory before it runs another program, so copying every page at fork time would waste most of the work. Modern kernels such as Linux let the parent and child share every page after the fork, and copy a page only when one of them writes to it.
Snapshots in file systems. A File System that never overwrites a block in place can take a snapshot of the whole disk almost for free: the snapshot is a second pointer to the old blocks, and later writes go to new blocks. Some file systems, such as ZFS and Btrfs, are built this way. Writes to a snapshotted file then cost one new block each, not a full copy of the file.
Serving large language models. When one prompt produces several sampled answers, the prompt's attention cache is the same for every answer. PagedAttention keeps that cache in fixed-size blocks that many answers can share, and it copies a block only when one answer writes new tokens into it. The paper's example describes exactly this rule, with the KV Cache as the data being shared.
How it works
Take one tiny instance. A page holds four cells with the values 5, 8, 2 and 7, and it lives in one physical frame, F1. The parent process and the child process each have a page-table entry that points to F1. The frame has a reference count, the number of page-table entries that point to it. Both entries are marked read-only, and the count starts at 1 before the fork.
- Share the frame. The fork makes the child's entry point to F1 too, so F1 now has a reference count of 2. Nothing was copied. Check: the four cells exist once in RAM, and two processes map them.
- Read. The child reads cell 1 and gets 8. The parent reads cell 1 and also gets 8. Reads need no copy. Check: both processes see the same value, which is correct.
- Write. The child writes 9 into cell 1. The entry is read-only, so the hardware raises a page fault before the write happens. Check: the write has not yet changed F1.
- Copy the frame. The kernel sees that F1 has a reference count of 2. It takes a free frame, F2, copies the four cells 5, 8, 2, 7 into it, and points the child's entry at F2 with write permission. Check: F2 holds the same four values, and F1 is untouched.
- Finish the write. The child now writes 9 into cell 1 of F2. The child reads 5, 9, 2, 7. The count of F1 drops to 1, because only the parent points to it. Check: the parent still reads 5, 8, 2, 7 from F1.
- The last owner writes freely. The parent writes 3 into cell 0. F1 has a count of 1, so no copy is needed. The kernel makes the parent's entry writable and writes in place. Check: nobody else can see F1, so no one is surprised.
flowchart LR P[Parent entry] --> F1["Frame F1 (count 1): 5 8 2 7"] C[Child entry] --> F2["Frame F2 (count 1): 5 9 2 7"]
The diagram shows the state after step 5. The rule behind all six steps is short: a shared frame is copied only when someone writes to it, and the writer gets the copy. The name comes from that rule.
The math
Each physical frame has a reference count , the number of page-table entries that point to it. When a process writes to a page that maps to frame :
- If , allocate a new frame , copy the bytes of into , point the writer at , and set and .
- If , write in place.
Here is the page size.
Step 1 in symbols: sharing costs no copies
Suppose a fork shares pages. Before any write, the two processes map the same frames, so frames are in use, not . The number of copies made so far is 0.
Step 4 in symbols: the cost of a write
Each shared page that anyone writes to is copied exactly once. The first writer finds and copies. A second writer finds the count already lowered, and if it is 1, writes in place. Let be the number of shared pages that are written. The bytes copied are
compared with for a full copy at fork time. The fraction copied is . When most pages are only read, this fraction is small.
Step 6 in symbols: the last owner
Once the count is 1, it stays 1 until a new share happens, and a write to that frame never copies. So each shared page costs at most one copy in its whole sharing life.
Worked examples
Use the instance from "How it works". The frame F1 holds four cells, and its count is 2 when the child writes.
- The child's write finds , which is greater than 1, so a copy is needed.
- The copy takes all 4 cells into F2, so 4 cells are copied.
- The count of F1 drops to 1, and the count of F2 is 1.
- Frames in use: 2 (F1 and F2), where before the write there was 1 shared frame.
- The parent's later write to F1 costs nothing extra, because .
Interpretation: the copy happened once, at the moment of the first write. The parent did not pay for a copy it never needed.
A process uses 1000 pages of 4 KiB each, so its memory is KiB. It forks, and the child writes to 3 of the pages.
- Copy at fork time, without copy-on-write: KiB is copied before the child runs.
- With copy-on-write, pages are written, so KiB is copied.
- Ratio: , which is 0.3% of the full copy.
- Frames in use right after the fork: 1000, not 2000.
- Frames in use after the writes: . The 3 new frames hold the child's changed pages, and the parent still uses the original 1000.
Interpretation: the child pays for the pages it touches. The 997 pages it never writes cost nothing beyond their page-table entries.
Assume a block holds 10 tokens. A prompt of 95 tokens fills 9 full blocks and one block that holds 5 tokens and has room for 5 more. The model then samples 4 answers from the same prompt. These numbers are illustrative, not measured.
- Without sharing, each answer keeps its own copy of the prompt: blocks.
- With sharing, the 9 full blocks are used by all four answers, and no answer ever writes into them, so they stay as 9 blocks.
- The partly filled block is also shared, with a count of 4. The first answer to write a new token into it copies the block (count 4 becomes 3). The second copies it (count 3 becomes 2). The third copies it (count 2 becomes 1). The fourth writes in place. That makes 4 blocks for the last position, one per answer.
- Prompt blocks with sharing: . Compared with 40, that is 32.5% of the blocks.
- Try it: the same four answers, but the prompt is 90 tokens, so every block is full. How many blocks does the prompt take without sharing, and how many with sharing?
Answer: without sharing blocks. With sharing, all 9 full blocks are shared and nothing is written into them, so the prompt takes 9 blocks and no copies are made.
Common mistakes and tradeoffs
It is tempting to think that copy-on-write makes every write slow. In fact only the first write to a shared page pays for a copy and a page fault. A page that the writer owns alone is written at the same speed as before, and a page nobody writes is never copied.
It is tempting to think that a fork copies the parent's memory. In fact it copies the page table, and the data is copied later, one page at a time, as writes happen. The page table is small compared with the data: with 8-byte entries and 4 KiB pages, it is of the data it describes.
It is tempting to think that copy-on-write always saves memory. In fact if both sides eventually write to every page, each page is copied once, and the program ends with two full copies plus the page-fault work. Then it has saved nothing and paid more than an eager copy would have cost.
| Situation | Problem | Better choice |
|---|---|---|
| Data is shared and mostly only read | Copying up front wastes time and memory | Copy-on-write |
| Most shared pages will be written | Every page is copied anyway, with extra faults | Copy eagerly at the start |
| Latency must be predictable | A page fault stalls the writer for a moment | Copy eagerly, or lock the pages in memory |
| Values are never changed at all | No copy is ever needed | Immutability, with plain sharing |
When to use it: owners share large data, most of it is only read, and the writes that do happen touch a small part. When not to use it: writes touch most of the data, or a stall of a page fault is unacceptable. In those cases copy eagerly, which is simpler to reason about.
History
Copy-on-write grew out of paged virtual memory. Once a system maps memory in pages through a table, changing one entry is enough to make two names share a frame, and the copy can be delayed until a write. Unix-style systems use this for fork, so a parent and child share memory until one writes. Later, file systems and databases used the same rule to take snapshots cheaply.
Recap
- Several owners can point at one frame, and a reference count tracks how many.
- A write to a shared page raises a page fault, and the kernel copies the frame for the writer.
- A write to a frame with a count of 1 happens in place, with no copy.
- Each shared page that is written is copied once, so the cost is bytes, where is the number of written pages.
- The same rule appears in process creation, in file-system snapshots and in the KV Cache of language models.
Read next: Layered File System, which builds on this rule for file layers, and PagedAttention, which applies it to cache blocks on a GPU.
Check yourself
QuestionRecall: what is a reference count, and what does a count of 1 mean for a write?Show answer
The reference count of a frame is the number of page-table entries that point to it. A count of 1 means only one owner uses the frame, so a write can change it in place without a copy.
QuestionComputation: a fork shares 500 pages of 4 KiB each, and the child writes to 7 of them. How many KiB are copied, and how many KiB would a full copy at fork time take?Show answer
With copy-on-write, KiB is copied. A full copy would take KiB. The copy-on-write cost is of the full copy.
QuestionWhy: why does the last owner of a shared frame never need a copy?Show answer
A copy exists so that the other owners keep the old contents. Once every other owner has been given its own copy, or has stopped using the frame, no one else can see the frame. Its count is 1, so a write in place changes nothing that another owner could read.
QuestionApplied: a team says that a snapshot of a 10 GB dataset is free under copy-on-write because nothing is copied at snapshot time. Under what workload is this claim wrong, and why?Show answer
The claim is wrong when the workload writes to nearly every block after the snapshot. Each written block is copied once, so the copies add up to nearly the full 10 GB, and the old and new versions both sit in memory or on disk. The snapshot saved work only at the start. It saves space only while the data changes little.