ArticleslgStudy

computer science

Copy-on-write

Copy-on-write is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Copy-on-write rather than just read about it. In short: Copy-on-write (COW), also called implicit sharing or shadowing, is a resource-management technique used in programming to manage shared data efficiently. Instead of copying data right away when multiple programs use it, the same data is shared between programs until one tries to modify it.

Key takeaways

  • Copy-on-write belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Copy-on-write to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Copy-on-write from memory before moving on to harder problems.

Reference excerpt

Copy-on-write (COW), also called implicit sharing or shadowing, is a resource-management technique used in programming to manage shared data efficiently. Instead of copying data right away when multiple programs use it, the same data is shared between programs until one tries to modify it. If no changes are made, no private copy is created, saving resources. A copy is only made when needed, ensuring each program has its own version when modifications occur. This technique is commonly applied to memory, files, and data structures.

Examples

In virtual memory management Copy-on-write finds its main use in operating systems, sharing the physical memory of computers running multiple processes, in the implementation of the fork() system call. Typically, the new process does not modify any memory and immediately executes a new process, replacing the address space entirely. It would waste processor time and memory to copy all of the old process's memory during the fork only to immediately discard the copy. Copy-on-write can be implemented efficiently using the page table by marking certain pages of memory as read-only and keeping a count of the number of references to the page. When data is written to these pages, the operating-system kernel intercepts the write attempt and allocates a new physical page, initialized with the copy-on-write data, although the allocation can be skipped if there is only one reference. The kernel then updates the page table with the new (writable) page, decrements the number of references, and performs the write. The new allocation ensures that a change in the memory of one process is not visible in another's. The copy-on-write technique can be extended to support efficient memory allocation by keeping one page of physical memory filled with zeros. When the memory is allocated, all the pages returned refer to the page of zeros and are all marked copy-on-write. This way, physical memory is not allocated for the process until data is written, allowing processes to reserve more virtual memory than physical memory and use memory sparsely, at the risk of running out of virtual address space. The combined algorithm is similar to demand paging. Copy-on-write pages are also used in the Linux kernel's same-page merging feature.

In software libraries and applications The string class provided by the C++ standard library was specifically designed to allow copy-on-write implementations in the initial C++98 standard, but C++11 disallowed copy-on-write string implementations:

In the PHP programming language, all types except references are implemented as copy-on-write. For example, strings and arrays are passed by reference, but when modified, they are duplicated if they have non-zero reference counts. This allows them to act as value types without the performance problems of copying on assignment or making them immutable. In the Qt framework, many types are copy-on-write ("implicitly shared" in Qt's terms). Qt uses atomic compare-and-swap operations to increment or decrement the internal reference counter. Since the copies are cheap, Qt types can often be safely used by multiple threads without the need of locking mechanisms such as mutexes. The benefits of COW are thus valid in both single- and multithreaded systems. Rust's standard library provides a Cow<T> ("clone-on-write") smart pointer type that wraps either borrowed or owned data. It allows a function to return borrowed data without cloning it when no modification is needed, but automatically clones the data into an owned, mutable value the first time it is written to. In Docker, a set of software for implementing operating-system level virtualization, docker images are built in a layered format, with lower layers being read-only and the upper layer available for editing. Creating a new image which shares the same base layers as another image does not copy the layers, but instead follows COW principles and allows the two images to share layers until one is edited. In the application Collabora Online, a collaborative office suite based on LibreOffice, documents open in their own process, isolating them from one another. A parent process called "forkit" loads the office suite once, and COW shares the memory with individual document processes. In 2026, Collabora Online introduced an optimization to prevent heap fragmentation from triggering unnecessary 4 KB page copies across forked document processes, reducing per-document RAM usage by up to 55%.

In computer storage COW is used as the underlying mechanism in file systems like ZFS, Btrfs, ReFS, APFS, and Bcachefs, as well as in logical volume management, and for various features in relational databases. In traditional file systems, modifying a file overwrites the original data blocks in place. In a copy-on-write (COW) file system, the original blocks may remain unchanged; when part of a file is modified, only the affected blocks are written to new locations, and metadata is updated to point to them, preserving the original version until it's no longer needed. This approach enables features like snapshots, which capture the state of a file at a specific time without consuming much additional space. Snapshots typically store only the modified data and are kept close to the original. However, they are considered a weak form of incremental backup and cannot replace a full backup. This approach also benefits flash storage media, such as SSDs. In order to create and start new containers quickly, container engines such as Docker perform copy-on-write in storage, either block-level copy-on-write (as described above) or file-level copy-on-write. Some but not all filesystems support file-level copy-on-write as part of union mounting, including OverlayFS, aufs, GlusterFS, and UnionFS.

See also Allocate-on-flush Dirty COW – a computer security vulnerability of the Linux kernel Flyweight pattern Memory management Persistent data structure Wear leveling PagedAttention – attention algorithm for efficient serving of Large language models

References

External links "A history of copy-on-write memory management". A keen grasp of the obvious. 21 January 2011. Retrieved 18 November 2024.

Worked examples

Example 1 — a first encounter with Copy-on-write

Start with the simplest possible case. Write down what Copy-on-write claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Copy-on-write before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Copy-on-write ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Copy-on-write

In research
Copy-on-write appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Copy-on-write in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Copy-on-write is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer data storage, Software optimization, Virtual memory, so understanding it makes those chapters shorter.
In everyday life
Look for Copy-on-write outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Copy-on-write” →

How to study Copy-on-write in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Copy-on-write means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Copy-on-write out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Copy-on-write in simple terms?

Copy-on-write (COW), also called implicit sharing or shadowing, is a resource-management technique used in programming to manage shared data efficiently. Instead of copying data right away when multiple programs use it, the same data is shared between programs until one tries to modify it.

Why does Copy-on-write matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Copy-on-write?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Copy-on-write.

Tags

  • Computer data storage
  • Software optimization
  • Virtual memory
  • Virtualization

Keep exploring