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). ?>
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Fantastisk!
Completion rate forbedret til 11.11
Copy on Write Mechanism
Sveip for å vise menyen
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). ?>
Takk for tilbakemeldingene dine!