Content deleted Content added
reworded explanation. +comma. checked URLs. |
Update storage description to make clear entire file is not copied when modified. Tags: Visual edit Mobile edit Mobile web edit |
||
(17 intermediate revisions by 11 users not shown) | |||
Line 1:
{{Short description|Programming technique for efficiently duplicating data}}
{{
'''Copy-on-write''' ('''COW'''),
==In virtual memory management==
Copy-on-write finds its main use in [[operating system]]s, sharing the [[physical memory]] of computers running multiple [[
Copy-on-write can be implemented efficiently using the [[page table]] by marking certain pages of [[
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]].<ref name="Linux" />
Copy-on-write pages are also used in the [[Linux kernel]]'s [[
==In software==
{{Expand section|date=October 2017}}
COW is also used in [[
===Examples===
The [[
<syntaxhighlight lang="cpp">
std::string x("Hello");
Line 30 ⟶ 28:
</syntaxhighlight>
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.<ref>{{cite web |last1=Pauli |first1=Julien |last2=Ferrara |first2=Anthony |last3=Popov |first3=Nikita |title=Memory management |date=2013 |url=https://www.phpinternalsbook.com/php5/zvals/memory_management.html#reference-counting-and-copy-on-write |website=PhpInternalsBook.com |access-date=
In the [[Qt (software)|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 [[
==In computer storage==
COW
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 [[Snapshot (computer storage)|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.<ref>{{cite web |last=Chien |first=Tim |title=Snapshots Are NOT Backups |url=https://www.oracle.com/database/technologies/rman-fra-snapshot.html |website=Oracle.com |publisher=Oracle |access-date=10 November 2023 |archive-date=10 November 2023 |archive-url=https://web.archive.org/web/20231110024434/https://www.oracle.com/database/technologies/rman-fra-snapshot.html |url-status=live }}</ref>
==See also==
Line 43:
* [[Memory management]]
* [[Persistent data structure]]
* [[Wear leveling]]
==References==
{{Reflist}}
==External links==
* {{cite web |title=A history of copy-on-write memory management |url=https://obvious.services.net/2011/01/history-of-copy-on-write-memory.html |website=A keen grasp of the obvious |access-date=18 November 2024 |date=21 January 2011}}
{{File systems}}
|