Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Copy on Write Mechanism | Reference Counting and Copy on Write
Practice
Projects
Quizzes & Challenges
Quizze
Challenges
/
PHP Memory Management

bookCopy on Write Mechanism

When working with arrays and objects, PHP uses a memory optimization called copy on write.

When you assign an array or object to another variable, PHP does not copy the data immediately. Both variables point to the same memory, and only the reference count increases. A real copy is created only when one of the variables is modified.

This saves memory and improves performance, especially with large datasets. You can pass arrays or objects to functions or assign them freely without extra memory cost, as long as the data is not changed.

copy_on_write_demo.php

copy_on_write_demo.php

copy
1234567891011121314151617181920212223242526
<?php // Demonstrating copy on write with arrays in PHP // Create a large array $array1 = range(1, 1000000); // Assign $array1 to $array2; no copy is made yet, both share the same data $array2 = $array1; // Check memory usage after assignment echo "Memory after assignment: " . memory_get_usage() . " bytes\n"; // Modify $array2; now copy on write occurs, and $array2 gets its own copy $array2[0] = 999; // Check memory usage after modification echo "Memory after modification: " . memory_get_usage() . " bytes\n"; // Output the first elements to show the arrays are now different echo "\$array1[0]: " . $array1[0] . "\n"; echo "\$array2[0]: " . $array2[0] . "\n"; // Explanation: // - When $array2 is assigned, both variables point to the same array data (no extra memory used). // - When $array2 is modified, PHP creates a separate copy for $array2 (memory increases). ?>
question mark

Which statement best describes how PHP's copy on write mechanism optimizes memory usage when assigning arrays or objects?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookCopy on Write Mechanism

Swipe um das Menü anzuzeigen

When working with arrays and objects, PHP uses a memory optimization called copy on write.

When you assign an array or object to another variable, PHP does not copy the data immediately. Both variables point to the same memory, and only the reference count increases. A real copy is created only when one of the variables is modified.

This saves memory and improves performance, especially with large datasets. You can pass arrays or objects to functions or assign them freely without extra memory cost, as long as the data is not changed.

copy_on_write_demo.php

copy_on_write_demo.php

copy
1234567891011121314151617181920212223242526
<?php // Demonstrating copy on write with arrays in PHP // Create a large array $array1 = range(1, 1000000); // Assign $array1 to $array2; no copy is made yet, both share the same data $array2 = $array1; // Check memory usage after assignment echo "Memory after assignment: " . memory_get_usage() . " bytes\n"; // Modify $array2; now copy on write occurs, and $array2 gets its own copy $array2[0] = 999; // Check memory usage after modification echo "Memory after modification: " . memory_get_usage() . " bytes\n"; // Output the first elements to show the arrays are now different echo "\$array1[0]: " . $array1[0] . "\n"; echo "\$array2[0]: " . $array2[0] . "\n"; // Explanation: // - When $array2 is assigned, both variables point to the same array data (no extra memory used). // - When $array2 is modified, PHP creates a separate copy for $array2 (memory increases). ?>
question mark

Which statement best describes how PHP's copy on write mechanism optimizes memory usage when assigning arrays or objects?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2
some-alt