PHP memory_get_peak_usage 和 ini_set ('memory_limit' , '-1' )

标签 php memory-management memory-limit

我最近遇到了内存分配问题,所以我开始尝试使用 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/

相关文章:

c - ANSI C 中的字符串处理(内存分配)

c++ - 卡在 boost 邻接列表清除

php - fatal error : Allowed memory size of 268435456 bytes exhausted (tried to allocate 71 bytes)

php - 文件句柄作为函数参数的类型提示

php - Mandrill 电子邮件附件文件路径

javascript - 刷新div而不用jquery重新加载页面

php - 如何在 MYSQL 中使用 REGEXP 来匹配数组中的日期?

c - free()之前是否需要检查指针是否为空

mongodb - 使用聚合时出现Mongo错误: sort exceeded memory limit

php - 无法增加 MAMP php 内存限制