php 是什么导致“无法为池分配内存”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3723316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is causing "Unable to allocate memory for pool" in PHP?
提问by jonathanatx
I've occasionally run up against a server's memory allocation limit, particularly with a bloated application like Wordpress, but never encountered "Unable to allocate memory for pool" and having trouble tracking down any information.
我偶尔会遇到服务器的内存分配限制,尤其是像 Wordpress 这样臃肿的应用程序,但从未遇到过“无法为池分配内存”并且无法跟踪任何信息。
Does anyone know what this means? I've tried increasing the memory_limit
without success. I also haven't made any significant changes to the application. One day there was no problem, the next day I hit this error.
有谁知道这是什么意思?我试过增加memory_limit
没有成功。我也没有对应用程序进行任何重大更改。一天没有问题,第二天我遇到了这个错误。
采纳答案by Frankie
For the people having this problem, please specify you .ini settings. Specifically your apc.mmap_file_mask setting.
对于遇到此问题的人,请指定您的 .ini 设置。特别是您的 apc.mmap_file_mask 设置。
For file-backed mmap, it should be set to something like:
对于文件支持的 mmap,它应该设置为:
apc.mmap_file_mask=/tmp/apc.XXXXXX
To mmap directly from /dev/zero, use:
要直接从 /dev/zero 映射,请使用:
apc.mmap_file_mask=/dev/zero
For POSIX-compliant shared-memory-backed mmap, use:
对于符合 POSIX 标准的共享内存支持的 mmap,请使用:
apc.mmap_file_mask=/apc.shm.XXXXXX
回答by bokan
Using a TTL of 0 means that APC will flush all the cachewhen it runs out of memory. The error don't appear anymore but it makes APC far less efficient. It's a no risk, no trouble, "I don't want to do my job" decision. APC is not meant to be used that way. You should choose a TTL high enough so the most accessed pages won't expire. The best is to give enough memory so APC doesn't need to flush cache.
使用 0 的 TTL 意味着 APC 将在内存不足时刷新所有缓存。该错误不再出现,但它使 APC 的效率大大降低。这是一个没有风险,没有麻烦,“我不想做我的工作”的决定。APC 不应该以这种方式使用。您应该选择一个足够高的 TTL,这样访问最多的页面就不会过期。最好是提供足够的内存,这样 APC 就不需要刷新缓存。
Just read the manual to understand how ttl is used : http://www.php.net/manual/en/apc.configuration.php#ini.apc.ttl
只需阅读手册以了解如何使用 ttl:http: //www.php.net/manual/en/apc.configuration.php#ini.apc.ttl
The solution is to increase memory allocated to APC. Do this by increasing apc.shm_size.
解决办法是增加分配给APC的内存。通过增加 apc.shm_size 来做到这一点。
If APC is compiled to use Shared Segment Memory you will be limited by your operating system. Type this command to see your system limit for each segment :
如果 APC 被编译为使用共享段内存,您将受到操作系统的限制。键入此命令以查看每个段的系统限制:
sysctl -a | grep -E "shmall|shmmax"
To alocate more memory you'll have to increase the number of segments with the parameter apc.shm_segments.
要分配更多内存,您必须使用参数 apc.shm_segments 增加段数。
If APC is using mmap memory then you have no limit. The amount of memory is still defined by the same option apc.shm_size.
如果 APC 使用 mmap 内存,那么您就没有限制。内存量仍然由相同的选项 apc.shm_size 定义。
If there's not enough memory on the server, then use filters option to prevent less frequently accessed php files from being cached.
如果服务器上没有足够的内存,则使用过滤器选项来防止缓存访问频率较低的 php 文件。
But never use a TTL of 0.
但永远不要使用 0 的 TTL。
As c33s said, use apc.php to check your config. Copy the file from apc package to a webfolder and point browser to it. You'll see what is really allocated and how it is used. The graphs must remain stable after hours, if they are completly changing at each refresh, then it means that your setup is wrong (APC is flushing everything). Allocate 20% more ram than what APC really use as a security margin, and check it on a regular basis.
正如 c33s 所说,使用 apc.php 检查您的配置。将文件从 apc 包复制到 web 文件夹并将浏览器指向它。您将看到真正分配的内容以及如何使用它。图表必须在数小时后保持稳定,如果它们在每次刷新时都完全改变,则意味着您的设置是错误的(APC 正在刷新所有内容)。分配比 APC 实际用作安全余量的 ram 多 20%,并定期检查。
The default of allowing only 32MB is ridiculously low. PHP was designed when servers were 64MB and most scripts were using one php file per page. Nowadays solutions like Magento require more than 10k files (~60Mb in APC). You should allow enough memory so most of php files are always cached. It's not a waste, it's more efficient to keep opcode in ram rather than having the corresponding raw php in file cache. Nowadays we can find dedicated servers with 24Gb of memory for as low as $80/month, so don't hesitate to allow several GB to APC. I put 2GB out of 24GB on a server hosting 5Magento stores and ~40 wordpress website, APC uses 1.2GB. Count 64MB for Magento installation, 40MB for a Wordpress with some plugins.
仅允许 32MB 的默认值低得离谱。PHP 是在服务器为 64MB 并且大多数脚本每页使用一个 php 文件时设计的。现在像 Magento 这样的解决方案需要超过 10k 个文件(在 APC 中约为 60Mb)。您应该留出足够的内存,以便始终缓存大多数 php 文件。这不是浪费,将操作码保存在 ram 中比将相应的原始 php 保存在文件缓存中更有效。如今,我们可以以低至 80 美元/月的价格找到具有 24Gb 内存的专用服务器,因此请毫不犹豫地为 APC 提供几 GB 内存。我在托管 5Magento 商店和大约 40 个 wordpress 网站的服务器上放置了 24GB 中的 2GB,APC 使用了 1.2GB。Magento 安装需要 64MB,带有一些插件的 Wordpress 需要 40MB。
Also, if you have developpment websites on the same server. Exclude them from cache.
此外,如果您在同一台服务器上有开发网站。从缓存中排除它们。
回答by c33s
solution for me:
我的解决方案:
- apc.ttl=0
- apc.shm_size=anything you want
- apc.ttl=0
- apc.shm_size=任何你想要的
edit start
编辑开始
warning!
警告!
@bokan indicated me that i should add a warning here.
@bokan 表示我应该在这里添加警告。
if you have a ttl of 0 this means the every cached item can be purged immediately. so if you have a small cache size like 2mb and a ttl of 0 this would render the apc useless, because the data in the cache gets always overwritten.
如果您的 ttl 为 0,这意味着可以立即清除每个缓存的项目。所以如果你有一个小的缓存大小,比如 2mb 和 0 的 ttl,这将使 apc 无用,因为缓存中的数据总是被覆盖。
lowering the ttl means only that the cache cannot become full, only with items which can't be replaced.
降低 ttl 仅意味着缓存不会变满,只有无法替换的项目。
so you have to choose a good balance between ttl and cache size.
所以你必须在 ttl 和缓存大小之间选择一个很好的平衡。
in my case i had a cache size of 1gb, so it was more than enough for me.
就我而言,我的缓存大小为 1GB,因此对我来说已经足够了。
edit end
编辑结束
had the same issue on centos 5 with php 5.2.17 and noticed that if the cache size is small and the ttl parameter is "high" (like 7200) while having a lot of php files to cache, then the cache fills up quite fast and apc doesn't find anything which it can remove because all files in the cache still fit in the ttl.
在带有 php 5.2.17 的 centos 5 上有同样的问题,并注意到如果缓存大小很小并且 ttl 参数是“高”(如 7200)同时有很多 php 文件要缓存,那么缓存会很快填满并且 apc 找不到任何可以删除的内容,因为缓存中的所有文件仍然适合 ttl。
increasing the memory size is only a part solution, you still run in this error if you cache fills up and all files are within the ttl.
增加内存大小只是部分解决方案,如果缓存已满并且所有文件都在 ttl 中,您仍然会遇到此错误。
so my solution was to set the ttl to 0, so apc fills up the cache an there is allways the possibility for apc to clear some memory for new data.
所以我的解决方案是将 ttl 设置为 0,这样 apc 就会填满缓存,而且 apc 总是有可能为新数据清除一些内存。
hope that helps
希望有帮助
edit: see also: http://pecl.php.net/bugs/bug.php?id=16966
编辑:另见:http: //pecl.php.net/bugs/bug.php?id=16966
download http://pecl.php.net/get/APCextract and run the apc.php, there you have a nice diagram how your cache usage look like
下载http://pecl.php.net/get/APC解压并运行 apc.php,你有一个很好的图表,你的缓存使用情况如何
回答by Brice D
Running the apc.php script is key to understanding what your problem is, IMO. This helped us size our cache properly and for the moment, seems to have resolved the problem.
运行 apc.php 脚本是了解您的问题的关键,IMO。这有助于我们正确调整缓存大小,目前似乎已经解决了问题。
回答by Brideau
For newbies like myself, these resources helped:
对于像我这样的新手,这些资源有帮助:
Finding the apc.ini file to make the changes recommended by c33s above, and setting recommended amounts: http://www.untwistedvortex.com/optimizing-tuning-apc-alternate-php-cache/
找到 apc.ini 文件以进行上述 c33s 推荐的更改,并设置推荐数量:http: //www.untwistedvortex.com/optimizing-tuning-apc-alternate-php-cache/
Understanding what apc.ttl is: http://www.php.net/manual/en/apc.configuration.php#ini.apc.ttl
了解什么是 apc.ttl:http://www.php.net/manual/en/apc.configuration.php#ini.apc.ttl
Understanding what apc.shm_size is: http://www.php.net/manual/en/apc.configuration.php#ini.apc.shm-size
了解什么是 apc.shm_size:http://www.php.net/manual/en/apc.configuration.php#ini.apc.shm-size
回答by Jase Whatson
As Bokan has mentioned, you can up the memory if available, and he is right on how counter productive setting TTL to 0 is.
正如 Bokan 所提到的,如果可用,您可以增加内存,他对将 TTL 设置为 0 的反效果是正确的。
NotE: This is how I fixed this error for my particular problem. Its a generic issue that can be caused by allot of things so only follow the below if you get the error and you think its caused by duplicate PHP files being loaded into APC.
注意:这是我针对特定问题修复此错误的方法。这是一个可能由分配的事情引起的通用问题,因此如果您收到错误并且您认为它是由重复的 PHP 文件加载到 APC 中引起的,请仅遵循以下步骤。
The issue I was having was when I released a new version of my PHP application. Ie replaced all my .php files with new ones APC would load both versions into cache.
我遇到的问题是当我发布新版本的 PHP 应用程序时。即用新的 APC 替换我所有的 .php 文件会将两个版本加载到缓存中。
Because I didnt have enough memory for two versions of the php files APC would run out of memory.
因为我没有足够的内存来存储两个版本的 php 文件,所以 APC 会耗尽内存。
There is a option called apc.stat to tell APC to check if a particular file has changed and if so replace it, this is typically ok for development because you are constantly making changes however on production its usually turned off as it was with in my case - http://www.php.net/manual/en/apc.configuration.php#ini.apc.stat
有一个名为 apc.stat 的选项可以告诉 APC 检查特定文件是否已更改,如果已更改,则替换它,这对于开发来说通常是可以的,因为您不断进行更改,但是在生产中通常会关闭它,就像在我的情况下一样案例 - http://www.php.net/manual/en/apc.configuration.php#ini.apc.stat
Turning apc.stat on would fix this issue if you are ok with the performance hit.
如果您对性能下降没问题,打开 apc.stat 将解决此问题。
The solution I came up with for my problem is check if the the project version has changed and if so empty the cache and reload the page.
我为我的问题提出的解决方案是检查项目版本是否已更改,如果已更改,请清空缓存并重新加载页面。
define('PROJECT_VERSION', '0.28');
if(apc_exists('MY_APP_VERSION') ){
if(apc_fetch('MY_APP_VERSION') != PROJECT_VERSION){
apc_clear_cache();
apc_store ('MY_APP_VERSION', PROJECT_VERSION);
header('Location: ' . 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
exit;
}
}else{
apc_store ('MY_APP_VERSION', PROJECT_VERSION);
}
回答by Peter Drinnan
This worked for our guys (running a slew of Wordpress sites on the same server).
这对我们的人有用(在同一台服务器上运行大量 Wordpress 站点)。
Changed memory settings in the /etc/php.d/apc.ini file. It was set to 64M, so we doubled it to 128M.
更改了 /etc/php.d/apc.ini 文件中的内存设置。它被设置为 64M,因此我们将其翻倍至 128M。
apc.shm_size=128M
apc.shm_size=128M
回答by andrew
on my system i had to insert apc.shm_size = 64M into /usr/local/etc/php.ini (FreeBSD 9.1) then when i looked at apc.php (which i copied from /usr/local/share/doc/APC/apc.php to /usr/local/www/apache24/data) i found that the cache size had increased from the default of 32M to 64M and i was no longer getting a large cache full count
在我的系统上,我不得不将 apc.shm_size = 64M 插入 /usr/local/etc/php.ini (FreeBSD 9.1) 然后当我查看 apc.php (我从 /usr/local/share/doc/APC 复制/apc.php 到 /usr/local/www/apache24/data) 我发现缓存大小已经从默认的 32M 增加到 64M 并且我不再获得大量缓存完整计数
references: http://au1.php.net/manual/en/apc.configuration.phpalso read Bokan's comments, they were very helpful
参考文献:http: //au1.php.net/manual/en/apc.configuration.php也阅读了 Bokan 的评论,他们非常有帮助
回答by Bialy7
To resolve this problem set value for apc.shm_size as integer Locate your apc.ini file (In my system apc.ini file location /etc/php5/conf.d/apc.ini) and set: apc.shm_size = 1000
要解决此问题,请将 apc.shm_size 的值设置为整数 找到您的 apc.ini 文件(在我的系统中 apc.ini 文件位置 /etc/php5/conf.d/apc.ini)并设置: apc.shm_size = 1000
回答by Brent Self
I received the error "Unable to allocate memory for pool" after moving an OpenCart installation to a different server. I also tried raising the memory_limit.
将 OpenCart 安装移动到其他服务器后,我收到错误“无法为池分配内存”。我也尝试提高 memory_limit。
The error stopped after I changed the permissions of the file in the error message to have write access by the user that apache runs as (apache, www-data, etc.). Instead of modifying /etc/group directly (or chmod-ing the files to 0777), I used usermod:
在我将错误消息中的文件权限更改为具有 apache 运行身份的用户(apache、www-data 等)的写访问权限后,错误停止了。我没有直接修改 /etc/group(或将文件修改为 0777),而是使用了 usermod:
usermod -a -G vhost-user-group apache-user
Then I had to restart apache for the change to take effect:
然后我必须重新启动 apache 才能使更改生效:
apachectl restart
Or
或者
sudo /etc/init.d/httpd restart
Or whatever your system uses to restart apache.
或者你的系统用来重启 apache 的任何东西。
If the site is on shared hosting, maybe you must change the file permissions with an FTP program, or contact the hosting provider?
如果该站点位于共享主机上,也许您必须使用 FTP 程序更改文件权限,或联系主机提供商?