What is causing "Unable to allocate memory for pool" in PHP?
Asked Answered
Z

12

134

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.

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.

Zarger answered 16/9, 2010 at 2:33 Comment(0)
L
90

Probably is APC related.

For the people having this problem, please specify you .ini settings. Specifically your apc.mmap_file_mask setting.

For file-backed mmap, it should be set to something like:

apc.mmap_file_mask=/tmp/apc.XXXXXX

To mmap directly from /dev/zero, use:

apc.mmap_file_mask=/dev/zero

For POSIX-compliant shared-memory-backed mmap, use:

apc.mmap_file_mask=/apc.shm.XXXXXX
Logogram answered 16/9, 2010 at 2:39 Comment(7)
Thanks! That's exactly the link I'd been looking for. Appreciate the help!Zarger
I've found that these changes don't fix the issue, as the comments on the linked thread also document...Trave
In my case even disabling APC doesn't help, so this is php-fpm problem it seems.Convulsive
More info for this APC setting: php.net/apc.configuration#ini.apc.mmap-file-maskAestival
In my case I had to change from file-backed to POSIX-compliant in order to get rid of the error.Tankard
I'm at a loss to understand how this answer solves the problem. Does the error happen when the file_mask is not one of these values? If I have one of these values and I am getting the error, do I need to switch it to a different one? Which one?Collimate
I am now freaked out, so my file-backed mmap was using this format: apc.mmap_file_mask=/tmp/apc.XXXXXX, then today I got these errors in all my websites and then needed to change the format to the POSIX-compliant as explained in here: apc.mmap_file_mask=/apc.shm.XXXXXX. I can assure that the only person who has access to the server is me and I did not change anything all to .ini files related to php...I am out of clue now and really confused of this issue, I don't want this to come over again when I sleep. I hope anyone can give an explanation on this, thanks.Seedling
C
125

Using a TTL of 0 means that APC will flush all the cache when 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.

Just read the manual to understand how ttl is used : 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.

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 :

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.

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.

If there's not enough memory on the server, then use filters option to prevent less frequently accessed php files from being cached.

But never use a TTL of 0.

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.

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.

Also, if you have developpment websites on the same server. Exclude them from cache.

Conant answered 27/8, 2012 at 16:43 Comment(6)
This! I'm running Wordpress and 32M was just not enough. Upped to 64M and now in the clear. Check apc.php people!Lanti
To increase to 64M you need to add apc.shm_size=64 and not apc.shm_size=64M (most example I've seen had an M at the end) Didn't work on my version of apc (v3.1.3p1)Linguistic
You are assuming that you will have plenty of cached files that have been in the cache longer than the TTL. c33s has an important point. If everything was recently accessed (let's say you have 70% of the cache that gets accessed all of the time like you want and have a big spike where a lot of extra infrequent files get added all at once), you are going to get errors thrown for TTL seconds. The cache is full, and you told APC that it shouldn't clear these entries so it complains. If you have TTL for 5 hours, you end up with 5 hours worth of errors waiting for those infrequent files to expire.Rus
@MatthewKolb : You should not allow caching more files than APC can hold in its memory. Use filters to prenvent unfrequently accessed files to be cached.Conant
@Patrick Forget: it depends.. on FreeBsd PHP was complaining when I didn't put the M (or G it said)Twibill
@PatrickForget - You're using a really old version of APC or something compiled for an alternate distro. All modern versions of APC expect M or G after the number as the old days of Megabytes only have left us.Cabalism
L
90

Probably is APC related.

For the people having this problem, please specify you .ini settings. Specifically your apc.mmap_file_mask setting.

For file-backed mmap, it should be set to something like:

apc.mmap_file_mask=/tmp/apc.XXXXXX

To mmap directly from /dev/zero, use:

apc.mmap_file_mask=/dev/zero

For POSIX-compliant shared-memory-backed mmap, use:

apc.mmap_file_mask=/apc.shm.XXXXXX
Logogram answered 16/9, 2010 at 2:39 Comment(7)
Thanks! That's exactly the link I'd been looking for. Appreciate the help!Zarger
I've found that these changes don't fix the issue, as the comments on the linked thread also document...Trave
In my case even disabling APC doesn't help, so this is php-fpm problem it seems.Convulsive
More info for this APC setting: php.net/apc.configuration#ini.apc.mmap-file-maskAestival
In my case I had to change from file-backed to POSIX-compliant in order to get rid of the error.Tankard
I'm at a loss to understand how this answer solves the problem. Does the error happen when the file_mask is not one of these values? If I have one of these values and I am getting the error, do I need to switch it to a different one? Which one?Collimate
I am now freaked out, so my file-backed mmap was using this format: apc.mmap_file_mask=/tmp/apc.XXXXXX, then today I got these errors in all my websites and then needed to change the format to the POSIX-compliant as explained in here: apc.mmap_file_mask=/apc.shm.XXXXXX. I can assure that the only person who has access to the server is me and I did not change anything all to .ini files related to php...I am out of clue now and really confused of this issue, I don't want this to come over again when I sleep. I hope anyone can give an explanation on this, thanks.Seedling
P
36

solution for me:

  • apc.ttl=0
  • apc.shm_size=anything you want

edit start

warning!

@bokan indicated me that i should add a warning here.

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.

lowering the ttl means only that the cache cannot become full, only with items which can't be replaced.

so you have to choose a good balance between ttl and cache size.

in my case i had a cache size of 1gb, so it was more than enough for me.

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.

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.

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.

hope that helps

edit: see also: http://pecl.php.net/bugs/bug.php?id=16966

download http://pecl.php.net/get/APC extract and run the apc.php, there you have a nice diagram how your cache usage look like

Polynices answered 14/2, 2011 at 20:49 Comment(9)
Thank you, this did help. I was getting about a dozen "Unable to allocate memory" errors per second. I doubled my cache size (32 to 64 MB) and dropped the ttl to 0. That completely removed these errors.Wallasey
This was the fix on our servers.Mayapple
This seemed to fix the problem for me as well.Lenity
Using ZWAMP and this seems to have done the trick as well. Thanks.Chervonets
This is not a solution ! The error disapears but APC will be almost disabled. It will flush all cache each time memory is full. Just read the manual Brideau's gave us. php.net/manual/en/apc.configuration.php#ini.apc.ttl.Conant
@Conant please read my full post. the solution is a combination of increasing the memory size and lowering the ttl. apc was not able to free memory because nothing was expired right now. you also can set the ttl to 10 or 50, this would also help. if you have a cache big enough, it just works as intended with ttl=0 and never "turns itself off", but having a to high ttl will result in this error. ttl=0 ensures that the cache can allways be cleard. a ttl of 0 with a cache size of 1MB would result in "disabling" apc.Polynices
You said "so my solution was to set the ttl to 0,". This is a no risk, no trouble, no responsibility taken decision. It must not be done, APC is not meant to be used this way. Common php CMS today use 20M or more. If you have 20 of them on a server with regular visits and only 256M allocated to APC the cache will fill up quickly too. It's important to say that allocating 1GB or more to APC is normal. APC save CPU cycles by using more memory. If you run out of memory, increase it, if you can't, disable APC for rarely accessed file with filter. But never use TTL of 0.Conant
@Conant added a warning in my answer.Polynices
It solved the problem for us too, after migrating to centOS we kept default ttl=7200 value and had the issue.Filippa
C
7

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.

Caulk answered 5/12, 2011 at 16:30 Comment(1)
as c33s said : download pecl.php.net/get/APC extract and run the apc.php, there you have a nice diagram how your cache usage look likeConant
T
4

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/

Understanding what apc.ttl is: 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

Trestlework answered 28/4, 2012 at 15:5 Comment(1)
Thanks, you pointed out the right solution. Lowering TTL is just like disabling APC.Conant
M
4

As Bokan has mentioned, you can up the memory if available, and he is right on how counter productive setting TTL to 0 is.

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.

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.

Because I didnt have enough memory for two versions of the php files APC would run out of memory.

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

Turning apc.stat on would fix this issue if you are ok with the performance hit.

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);
}
Misbehavior answered 12/11, 2012 at 4:45 Comment(0)
C
2

This worked for our guys (running a slew of Wordpress sites on the same server).

Changed memory settings in the /etc/php.d/apc.ini file. It was set to 64M, so we doubled it to 128M.

apc.shm_size=128M

Cleres answered 10/3, 2014 at 21:29 Comment(0)
P
1

Looking at the internets there can be various of causes. In my case leaving everything default except...

apc.shm_size = 64M

...cleared the countless warnings that I was getting earlier.

Provide answered 25/8, 2012 at 11:58 Comment(0)
P
1

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.

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:

usermod -a -G vhost-user-group apache-user

Then I had to restart apache for the change to take effect:

apachectl restart

Or

sudo /etc/init.d/httpd restart

Or whatever your system uses to restart apache.

If the site is on shared hosting, maybe you must change the file permissions with an FTP program, or contact the hosting provider?

Phonologist answered 7/10, 2013 at 0:32 Comment(0)
V
1

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

Vanadinite answered 1/3, 2014 at 14:35 Comment(0)
C
1

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

references: https://www.php.net/manual/en/apc.configuration.php also read Bokan's comments, they were very helpful

Camper answered 26/5, 2014 at 2:44 Comment(0)
G
0

Monitor your Cached Files Size (you can use apc.php from apc pecl package) and increase apc.shm_size according to your needs.

This solves the problem.

Galileo answered 11/10, 2013 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.