我听说php使用copy-on-write:
http ://en.wikipedia.org/wiki/Copy-on-write
举个例子:
<?
for($i=0;$i<100000;$i++)
$x[] = $i;
// we output the memory use:
echo memory_get_usage().'<br/>'; // outputs 14521040
// here we equate $y to $x, but instead of creating a copy,
// php engine just creates a pointer to the same memory space
$y = $x;
echo memory_get_usage().'<br/>'; // outputs 14521128
// here we change something in y, now php engine
// "creates a seperate copy" for y and makes the change
$y[1]=8;
echo memory_get_usage().'<br/>'; // outputs 23569904
?>
以及函数调用的类似行为:
<?
for($i=0;$i<100000;$i++)
$x[] = $i;
echo memory_get_usage().'<br/>'; /* 14524968 */
function abc($y){
echo memory_get_usage().'<br/>'; /* 14524968 */
$y[1] = 1;
echo memory_get_usage().'<br/>'; /* 23573752 */
$z[]= $y;
echo memory_get_usage().'<br/>'; /* 23574040 */
}
abc($x);
echo memory_get_usage().'<br/>'; /* 14524968 */
?>
PS:我在windows上测试这个,可能在linux上不一样