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

发布于 2024-11-06 09:30:35 字数 269 浏览 1 评论 0原文

我想知道 PHP 何时释放用于变量的内存,

例如

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

它是否比以下速度慢

function foo(){
  return 'data'; 
}

I was wondering when does PHP free the memory which is used for a variables

for example

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

is it slower than:

function foo(){
  return 'data'; 
}

?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜司空 2024-11-13 09:30:35

好吧,让我们来看看吧!

<?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 的初始调用期间内存的增加很可能是由于创建造成的存储内存使用的变量。

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

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

Well, let's find out!

<?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;
}

Under PHP 5.3.6, my output is:

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
)

and then

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

It's very likely that the memory increase during the initial calls to demo1 and demo2 that discard the output is due to the creation of variables to store memory use.

However, the bottom line here is the two storage examples, where both returning data directly and assigning it to a variable before returning it resulted in the same exact memory use for the given data.

Conclusion: PHP seems smart enough in this simple test to not needlessly copy string variables -- though do keep an eye on the memory use difference between the two functions. Just declaring the demo1 function took more memory that declaring demo2. A few hundred bytes, really.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文