Copy-on-write

Copy-on-write (COW), also called implicit sharing[1] or shadowing,[2] is a resource-management technique[3] 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.[3] 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.

## Contents

* 1 In virtual memory management
* 2 In software
* 2.1 Examples
* 3 In computer storage
* 4 See also
* 5 References
* 6 External links

## 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.[4]

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.[3]

Copy-on-write pages are also used in the Linux kernel's same-page merging feature.[5]

## In software

COW is also used in library, application, and system code.

### Examples

The string class provided by the C++ standard library was specifically designed to allow copy-on-write implementations in the initial C++98 standard,[6] but not in the newer C++11 standard:[7]

std::string x("Hello");

std::string y = x;  // x and y use the same buffer.

y += ", World!";    // Now y uses a different buffer; x still uses the same old buffer.

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.[8]

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.[9]

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.[10][11]

## In computer storage

COW is used as the underlying mechanism in file systems like ZFS, Btrfs,[12] ReFS, and Bcachefs, as well as in logical volume management and database servers such as Microsoft SQL Server.

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 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.[13]

In order to create and start new containers quickly, container engines doing OS-level virtualization often 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,[14] including OverlayFS, aufs, GlusterFS, and UnionFS.

## See also

* Allocate-on-flush
* Dirty COW – a computer security vulnerability for the Linux kernel
* Flyweight pattern
* Memory management
* Persistent data structure
* Wear leveling

## References

1. ↑ "Implicit Sharing". https://doc.qt.io/qt-5/implicit-sharing.html.
2. ↑ Rodeh, Ohad (1 February 2008). "B-Trees, Shadowing, and Clones". ACM Transactions on Storage 3 (4): 1. doi:10.1145/1326542.1326544. http://liw.fi/larch/ohad-btrees-shadowing-clones.pdf. Retrieved 10 November 2023.
3. ↑ 3.0 3.1 3.2 Bovet, Daniel Pierre; Cesati, Marco (1 January 2002). Understanding the Linux Kernel. O'Reilly Media. p. 295. ISBN 9780596002138. https://books.google.com/books?id=9yIEji1UheIC&q="copy on write"&pg=PA295. Retrieved 10 November 2023.
4. ↑ Silberschatz, Abraham; Galvin, Peter B.; Gagne, Greg (2018). Operating System Concepts (10th ed.). Wiley. pp. 120–123. ISBN 978-1119456339.
5. ↑ Abbas, Ali. "The Kernel Samepage Merging Process". http://alouche.net/blog/2011/07/18/the-kernel-samepage-merging-process/.
6. ↑ Meyers, Scott (2012). Effective STL. Addison-Wesley. pp. 64–65. ISBN 9780132979184.
7. ↑ "Concurrency Modifications to Basic String". https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2534.html.
8. ↑ Pauli, Julien; Ferrara, Anthony; Popov, Nikita (2013). "Memory management". https://www.phpinternalsbook.com/php5/zvals/memory_management.html#reference-counting-and-copy-on-write.
9. ↑ "Threads and Implicitly Shared Classes". https://doc.qt.io/qt-5/threads-modules.html#threads-and-implicitly-shared-classes.
10. ↑ "Storage drivers" (in en). 2025-11-13. https://docs.docker.com/engine/storage/drivers/.
11. ↑ Tiwari, Anjaneya (2024-12-18). "🔍 Understanding Docker Storage: Image Layers, Copy-on-Write, and How Data Changes Work" (in en). https://medium.com/@princetiwari97940/understanding-docker-storage-image-layers-copy-on-write-and-how-data-changes-work-caf38c2a3477.
12. ↑ Kasampalis, Sakis (2010). "Copy-on-Write Based File Systems Performance Analysis and Implementation". p. 19. https://sakisk.me/files/copy-on-write-based-file-systems.pdf.
13. ↑ Chien, Tim. "Snapshots Are NOT Backups". Oracle. https://www.oracle.com/database/technologies/rman-fra-snapshot.html.
14. ↑ Neil Brown. "Overlay Filesystem".

## External links

* "A history of copy-on-write memory management". 21 January 2011. https://obvious.services.net/2011/01/history-of-copy-on-write-memory.html.

* v
* t
* e

File systems

* Comparison of file systems
* distributed
* Unix filesystem

Disk|

* ADFS
* AdvFS
* Amiga FFS
* Amiga OFS
* APFS
* AthFS
* bcachefs
* BFS
* Be File System
* Boot File System
* Btrfs
* CVFS
* CXFS
* DFS
* EFS
* Encrypting File System
* Extent File System
* Episode
* ext
* ext2
* ext3
* ext3cow
* ext4
* FFS/FFS2
* FAT
* exFAT
* Files-11
* Fossil
* HAMMER
* HAMMER2
* HFS
* HFS+
* HPFS
* HTFS
* IBM Spectrum Scale
* JFS
* LFS
* MFS
* Macintosh File System
* TiVo Media File System
* MINIX
* NetWare File System
* Next3
* NILFS
* NILFS2
* NSS
* NTFS
* OneFS
* PFS
* QFS
* QNX4FS
* ReFS
* ReiserFS
* Reiser4
* Reliance
* Reliance Nitro
* RFS
* SFS
* SNFS
* Soup (Apple)
* Tux3
* UBIFS
* UFS
* soft updates
* WAPBL
* VxFS
* WAFL
* Xiafs
* XFS
* Xsan
* zFS
* ZFS

Optical disc|

* HSF
* ISO 9660
* ISO 13490
* UDF

Flash memory and SSD|

* APFS
* FAT
* exFAT
* CHFS
* TFAT
* EROFS
* FFS2
* F2FS
* HPFS
* JFFS
* JFFS2
* JFS
* LogFS
* NILFS
* NILFS2
* NVFS
* YAFFS
* UBIFS

Distributed|

* CXFS
* GFS2
* Google File System
* OCFS2
* OrangeFS
* PVFS
* QFS
* Xsan
* more...

NAS|

* AFS (OpenAFS)
* AFP
* Coda
* DFS
* Google File System
* IBM Spectrum Scale
* Lustre
* NCP
* NFS
* POHMELFS
* Hadoop
* SMB (CIFS)
* SSHFS
* more...

Specialized|

* Aufs
* AXFS
* Boot File System
* CDfs
* Compact Disc File System
* cramfs
* Davfs2
* EROFS
* FTPFS
* FUSE
* Lnfs
* LTFS
* NOVA
* MVFS
* SquashFS
* UMSDOS
* OverlayFS
* UnionFS
* WBFS

Pseudo and virtual|

* configfs
* devfs
* debugfs
* kernfs
* procfs
* specfs
* sysfs
* tmpfs
* WinFS

Encrypted|

* eCryptfs
* EncFS
* EFS
* Rubberhose
* SSHFS
* ZFS

Types|

* Clustered
* Global
* Grid
* Self-certifying
* Flash
* Journaling
* Log-structured
* Object
* Record-oriented
* Semantic
* Steganographic
* Synthetic
* Versioning

Features|

* Case preservation
* Copy-on-write
* Data deduplication
* Data scrubbing
* Execute in place
* Extent
* File attribute
* Extended file attributes
* File change log
* Fork
* Links
* Hard
* Symbolic

Access control|

* Access control list
* Filesystem-level encryption
* Permissions
* Modes
* Sticky bit

Interfaces|

* File manager
* File system API
* Installable File System
* Virtual file system

Lists|

* Cryptographic
* Default
* Log-structured

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Copy-on-write. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Copy-on-write&oldid=4229375"
*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template