Copy 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
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). ?>
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Geweldig!
Completion tarief verbeterd naar 11.11
Copy on Write Mechanism
Veeg om het menu te tonen
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
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). ?>
Bedankt voor je feedback!