简体   繁体   English

php memory_limit问题

[英]php memory_limit issue

one of my script required large memory. 我的脚本之一需要很大的内存。 and probably because of this I am getting following error 可能正因为如此,我得到以下错误

Fatal error: Out of memory (allocated 42729472) (tried to allocate 32 bytes) 致命错误:内存不足(已分配42729472)(试图分配32个字节)

I tried to change memory_limit by using ini_set('memory_limit', '256M'); 我试图通过使用ini_set('memory_limit','256M')来更改memory_limit; on the top of my page but still I am getting same issues. 在页面顶部,但仍然遇到相同的问题。 I also tried ini_set('memory_limit', '512M'); 我也尝试过ini_set('memory_limit','512M'); but no luck. 但没有运气。

Do you have access to the php.ini file? 您可以访问php.ini文件吗? Then change the memory_limit value there. 然后在那里更改memory_limit的值。

You should also analyze your script. 您还应该分析您的脚本。 Lots of database transactions? 数据库事务很多? Many of them in loops? 他们中的许多人循环吗? Lots of loops? 很多循环? Lots of temporary variables? 很多临时变量? A clever usage of unset can make a huge difference. 巧妙地使用unset可以产生巨大的变化。

Also nice to know: Performance Considerations and Garbage Collection 也很高兴知道: 性能注意事项垃圾回收

Edit (a possible answer to your question why memory_limit doesn't work with ini_set ) 编辑(您的问题的可能答案,为什么memory_limit不能与ini_set

memory_limit integer This sets the maximum amount of memory in bytes that a script is allowed to allocate. memory_limit整数设置允许脚本分配的最大内存量(以字节为单位)。 This helps prevent poorly written scripts for eating up all available memory on a server. 这有助于防止编写不良的脚本耗尽服务器上的所有可用内存。 Note that to have no memory limit, set this directive to -1. 请注意,没有内存限制,请将此伪指令设置为-1。

Prior to PHP 5.2.1, in order to use this directive it had to be enabled at compile time by using --enable-memory-limit in the configure line. 在PHP 5.2.1之前,要使用此指令,必须在编译时通过在configure行中使用--enable-memory-limit来启用它。 This compile-time flag was also required to define the functions memory_get_usage() and memory_get_peak_usage() prior to 5.2.1. 在5.2.1之前,还需要使用此编译时标志来定义函数memory_get_usage()和memory_get_peak_usage()。

When an integer is used, the value is measured in bytes. 使用整数时,该值以字节为单位。 Shorthand notation, as described in this FAQ, may also be used. 也可以使用本常见问题解答中所述的速记符号。

source: php.net 资料来源: php.net

That's crashing after about 42M, which suggests to me that the PHP interpreter didn't get the memo. 在大约4千2百万之后崩溃了,这向我暗示PHP解释器没有得到备忘录。 Try the same ini_set() in a test page with the phpinfo() output and look for the memory limit. 在测试页面中使用phpinfo()输出尝试相同的ini_set()并查找内存限制。

Can you set it in the php.ini file instead? 您可以在php.ini文件中进行设置吗?

I was having a similar issue earlier. 我之前也遇到过类似的问题 In my case it was the suhosin patch preventing my script from acquiring more memory. 就我而言,这是suhosin补丁程序,阻止了我的脚本获取更多内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM