我最近遇到了内存分配问题,所以我开始尝试使用 ini_set('memory_limit', value);
指令,尝试以增量方式输入值。现在,通过网络(和 SO)搜索我发现我可以将 -1
作为 value
。所以,我做到了,现在脚本完全运行到最后没有中断(在我以前得到内存分配错误之前)。
然而,我不明白的是,脚本文件末尾的这两行:
$mem = memory_get_peak_usage(true);
echo "Peak mem. usage: <b>" . round($mem / 1024 / 10124, 2) . "</b> MB";
产生大约 10.8MB,当我查看 /var/log/messages
时,我可以看到这一行:
Nov 21 13:52:26 mail suhosin[1153]: ALERT-SIMULATION - script tried to increase
memory_limit to 4294967295 bytes which is above the allowed value (attacker
'xx.xxx.xxx.xxx', file '/var/www/html/file.php', line 5)
这意味着脚本试图分配4096MB!
这怎么可能?而且,我最感兴趣的是为什么在这种情况下脚本执行没有停止?是因为 ini_set('memory_limit', '-1');
吗?我的意思是,我确实读过不推荐将 -1
作为 value
并且我知道问题出在脚本中(一次读取太多数据)内存),我将通过顺序读取来修复它,但我对这些数据差异感到困惑,所以如果有人能对此有所了解,我将不胜感激。
最佳答案
这是因为 suhosin 补丁使用了它自己的“硬”最大内存限制,suhosin.memory_limit
.
来自配置引用:
Suhosin [...] disallows setting the
memory_limit
to a value greater than the one the script started with, when this option is left at 0.
换句话说,如果您更改 memory_limit
使其大于 suhosin 的上限,那么它会简单地假定您是试图做一些可疑事情的“攻击者”。
关于PHP memory_get_peak_usage 和 ini_set ('memory_limit' , '-1' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13495281/