gpt4 book ai didi

php - PHP 如何为变量分配和释放内存?

转载 作者:IT王子 更新时间:2023-10-28 23:32:44 27 4
gpt4 key购买 nike

我想知道 PHP 什么时候释放用于变量的内存

例如

function foo(){
$foo = 'data';
return $foo; // <- is the memory space for `$foo` emptied at this point?
}

是否比:

function foo(){
return 'data';
}

?

最佳答案

好吧,让我们找出答案!

<?php
$checkpoints = array( 'start' => memory_get_usage() );

$checkpoints['before defining demo1'] = memory_get_usage();
function demo1() { $foo = 'data'; return $foo; }
$checkpoints['after defining demo1'] = memory_get_usage();

$checkpoints['before defining demo2'] = memory_get_usage();
function demo2() { return 'data'; }
$checkpoints['after defining demo2'] = memory_get_usage();


$checkpoints['before calling demo1'] = memory_get_usage();
demo1();
$checkpoints['after calling demo1'] = memory_get_usage();

$checkpoints['before calling demo2'] = memory_get_usage();
demo2();
$checkpoints['after calling demo2'] = memory_get_usage();

$checkpoints['before calling demo1 with storage'] = memory_get_usage();
$storage1 = demo1();
$checkpoints['after calling demo1 with storage'] = memory_get_usage();

$checkpoints['before calling demo2 with storage'] = memory_get_usage();
$storage2 = demo2();
$checkpoints['after calling demo2 with storage'] = memory_get_usage();

echo '<pre>';
print_r($checkpoints);

$last_key = 'start';
foreach($checkpoints as $key => $value) {
echo "{$key} - {$last_key} = ", ($value - $checkpoints[$last_key]), "\n";
$last_key = $key;
}

在 PHP 5.3.6 下,我的输出是:

Array
(
[start] => 321920
[before defining demo1] => 322188
[after defining demo1] => 322788
[before defining demo2] => 322880
[after defining demo2] => 323188
[before calling demo1] => 323280
[after calling demo1] => 323368
[before calling demo2] => 323464
[after calling demo2] => 323552
[before calling demo1 with storage] => 323692
[after calling demo1 with storage] => 323896
[before calling demo2 with storage] => 324000
[after calling demo2 with storage] => 324204
)

然后

start - start = 0
before defining demo1 - start = 268
after defining demo1 - before defining demo1 = 600
before defining demo2 - after defining demo1 = 92
after defining demo2 - before defining demo2 = 308
before calling demo1 - after defining demo2 = 92
after calling demo1 - before calling demo1 = 88
before calling demo2 - after calling demo1 = 96
after calling demo2 - before calling demo2 = 88
before calling demo1 with storage - after calling demo2 = 140
after calling demo1 with storage - before calling demo1 with storage = 204
before calling demo2 with storage - after calling demo1 with storage = 104
after calling demo2 with storage - before calling demo2 with storage = 204

在初始调用 demo1demo2 丢弃输出的过程中,内存增加很可能是由于创建了用于存储内存使用的变量。

但是,这里的底线是两个存储示例,其中直接返回数据并在返回之前将其分配给变量导致给定数据的完全相同的内存使用。 p>

结论:在这个简单的测试中,PHP 似乎足够智能 不会不必要地复制字符串变量——尽管请注意两个函数之间的内存使用差异。仅声明 demo1 函数会比声明 demo2 占用更多内存。几百字节,真的。

关于php - PHP 如何为变量分配和释放内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5971090/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com