我需要一个PHP CLI脚本来通过GD函数进行各种图像转换。图像非常大,所以我需要尽可能多地挤压内存。但是,imagedestroy()似乎不会在被问到时释放内存。
请考虑以下演示脚本' test.php':
#!/usr/bin/php5
<?php
$memory = [0=> memoryCheck()];
$res1 = imagecreatetruecolor(8192, 4096);
memoryReport('Res1 created');
$res2 = imagecreatetruecolor(8192, 4096);
memoryReport('Res2 created');
imagedestroy($res1);
memoryReport('Res1 destroyed');
imagedestroy($res2);
memoryReport('Res2 destroyed');
// memory reporting functions follow:
function memoryCheck()
{
return (int) trim(substr(shell_exec('free -b'), 166, 11));
}
function format($value)
{
$val = \abs($value);
$unit=array('B','kiB','MiB','GiB','TiB','PiB');
return @round($val/pow(1024,($i=floor((($val==0)?0:log($val,1024))))),2).' '.$unit[$i];
}
function memoryReport($msg)
{
global $memory;
$start = $memory[0];
$prev = end($memory);
$now = memoryCheck();
echo sprintf("%s: %s (%s)\n",
$msg,
format($now-$start),
(($diff=$now-$prev) <0) ? '-'. format($diff) : '+'. format($diff)
);
$memory[] = $now;
}
?>
让我们通过发出:
来运行它free -m; ./ test.php; free -m
示例结果:
total used free shared buffers cached
Mem: 7890 7072 818 561 218 2497
-/+ buffers/cache: 4355 3534
Swap: 8299 0 8299
Res1 created: 109.76 MiB (+109.76 MiB)
Res2 created: 218.77 MiB (+109.01 MiB)
Res1 destroyed: 218.9 MiB (+140 kiB)
Res2 destroyed: 888 kiB (-218.04 MiB)
total used free shared buffers cached
Mem: 7890 7072 817 561 218 2498
-/+ buffers/cache: 4356 3534
Swap: 8299 0 8299
如您所见,创建一个图像的成本为109-110MB。创建第二个后,我们已经用完了两倍。但是首先摧毁并不会释放记忆。所有资源&#39;只有在两个图像都被破坏后才会释放内存。
为什么呢?我忽略了什么吗?我该怎么做才能修改它?
更新: 我添加了将$ res1设置为null然后完全取消设置。代码:
$memory = [0=> memoryCheck()];
$res1 = imagecreatetruecolor(8192, 4096);
memoryReport('Res1 created');
$res2 = imagecreatetruecolor(8192, 4096);
memoryReport('Res2 created');
imagedestroy($res1);
memoryReport('Res1 destroyed');
$res1 = null;
memoryReport('Res1 is null');
unset($res1);
memoryReport('Res1 is unset');
imagedestroy($res2);
memoryReport('Res2 destroyed');
现在的结果是:
Res1 created: 109.48 MiB (+109.48 MiB)
Res2 created: 219.33 MiB (+109.85 MiB)
Res1 destroyed: 219.5 MiB (+168 kiB)
Res1 is null: 220.15 MiB (+668 kiB)
Res1 is unset: 220.38 MiB (+232 kiB)
Res2 destroyed: 2 MiB (-218.36 MiB)
另外,我添加了
gc_collect_cycles();
memoryReport('GC collect');
在imagedestroy($ res2)之前;事情变得很奇怪:
Res1 created: 109.59 MiB (+109.59 MiB)
Res2 created: 219.08 MiB (+109.5 MiB)
Res1 destroyed: 219.21 MiB (+132 kiB)
Res1 is null: 219.36 MiB (+148 kiB)
Res1 is unset: 219.75 MiB (+408 kiB)
GC collect: 220.57 MiB (+836 kiB)
Res2 destroyed: 220.46 MiB (-108 kiB)
根据&#39;免费&#39;命令,只有在脚本结束后才会释放内存。
答案 0 :(得分:0)
我想知道它是否是GC ...尝试在gc_collect_cycles()
之后运行imagedestroy()
。远射,但你永远不知道...