Fatal Error Unable to allocate shared memory segment 128MB

General discussion on PHP
Post Reply
will3390
Posts: 6
Joined: Mon Oct 07, 2013 12:04 pm

Fatal Error Unable to allocate shared memory segment 128MB

Post by will3390 » Fri Oct 11, 2013 8:22 pm

I'm running ZendOPcache 7.0.2 along with APCu 4.0.2 and the ionCube PHP Loader v4.2.2 on my Linux server running Apache 2.2.24 with PHP 5.4.20.

The default memory consumption setting for opcache is 128 (MB) and is set like this in opcache.ini:

opcache.memory_consumption=128

After etting opcache.memory_consumption=128 on my server and browsing several websites hosted on it, I eventually run into Internal Server Errors and find this error (or similar) in my error log:

(104)Connection reset by peer: mod_fcgid: error reading data from FastCGI server
Premature end of script headers: index.php

In fact, when opcache.memory_consumption is set any higher than 32, I can eventually produce Internal Server Errors. Then, I see this Fatal Error if I run the command "php -v" in a terminal window:

Fatal Error Unable to allocate shared memory segment of 134217728 bytes: mmap: No space left on device (28)

All my pages are .php pages, some with HTML on them.

As long as opcache.memory_consumption=16 or opcache.memory_consumption=32, my websites function normally. When things are running normally, running php -v in a terminal window shows Zend OPcache loaded correctly:

PHP 5.4.20 (cli) (built: Sep 24 2013 11:14:50)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
with the ionCube PHP Loader v4.2.2, Copyright (c) 2002-2012, by ionCube Ltd., and
with Zend OPcache v7.0.2, Copyright (c) 1999-2013, by Zend Technologies

Does anyone know why I cannot set the default opcache.memory_consumption=128 on my server with the Fatal Error and FastCGI errors in my error.log file? And, how can I fix it?

mark397
Posts: 87
Joined: Thu May 30, 2013 6:12 pm

Re: Fatal Error Unable to allocate shared memory segment 128

Post by mark397 » Fri Oct 11, 2013 8:49 pm

134217728/1024^2 = 128 (MB)
You're hitting that 128M limit. When you set your shared memory cache size to larger you'll need to set the global memory limit higher. You likely have set memory_limit = 32M - you'll need to up this along with the cache limit, and there should be some breathing room between the cache limit and memory limit, although not much should be needed. Of course this all depends on the size of your cache and the size of your application. You may also be attempting to cache more than your set 128M limit. How much space do you really use?

More performance tweaking variables you'll want to check into:
  • exit_on_timeout
  • max_input_nesting_level
  • max_input_vars
  • realpath_cache_size
  • zend.enable_gc
  • max_execution_time
  • max_input_time
  • realpath_cache_ttl

will3390
Posts: 6
Joined: Mon Oct 07, 2013 12:04 pm

Re: Fatal Error Unable to allocate shared memory segment 128

Post by will3390 » Fri Oct 11, 2013 9:29 pm

My memory_limit is set to 128M in php.ini and these are the other settings for the directives you've listed:

exit_on_timeout=Off
max_input_nesting_level=64
max_input_vars=1000
realpath_cache_size-16K
zend.enable_gc=On
max_execution_time=60
max_input_time=60
realpath_cache_ttl=120

These are the default directives that ZendOPcache's README file specifies:

opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1

I tried setting opcache.memory_consumption=64 and my server seemed to like that just fine (for a few minutes). Then, running the "php -v" command in terminal again reported the Fatal Error.Though, I'm not seeing the Internal Server Errors when viewing webpages, like I do when opcache.memory_consumption=128.

I spoke with a tech at my web hosting company. He has his php memory_limit=128M and opcache.memory_consumption=128 and he doesn't have this problem on his server. So, perhaps my websites (one in particular that I have narrowed it down to) are using up more than the allotted 128M of memory, but I don't know how to determine how much more than 128 that particular site is needs. But, my tech guy says that having both those settings at 128 should NOT cause the Fatal Error message.

will3390
Posts: 6
Joined: Mon Oct 07, 2013 12:04 pm

Re: Fatal Error Unable to allocate shared memory segment 128

Post by will3390 » Fri Oct 11, 2013 11:30 pm

There's something about one particular website on my server that immediately generates Internal Server Errors when browsing the site after setting opcache.memory_consumption=128 and restarting apache. Then, the following error appears in my server error log:

(104)Connection reset by peer: mod_fcgid: error reading data from FastCGI server
Premature end of script headers: index.php

The other sites on the server operate fine with opcache.memory_consumption=128, with no Internal Server Errors. I'm not sure how to diagnose the problem with the information I have…

Are there fcgi configuration settings that need to be tweaked for this particular domain, perhaps?

mark397
Posts: 87
Joined: Thu May 30, 2013 6:12 pm

Re: Fatal Error Unable to allocate shared memory segment 128

Post by mark397 » Tue Oct 15, 2013 7:04 pm

That's where I would look first, but unfortunately I'm not familiar with fcgi (I only run Zendserver on iSeries, and on Linux I run a more vanilla Apache). Perhaps the config is wrong or possibly fcgi doesn't exist on that system? I checked my package manager on my Linux box and apache2-mod-fcgid is not installed by default.

will3390
Posts: 6
Joined: Mon Oct 07, 2013 12:04 pm

Re: Fatal Error Unable to allocate shared memory segment 128

Post by will3390 » Tue Oct 15, 2013 7:07 pm

FCGI is definitely installed on the system and running.

tedtiger
Posts: 244
Joined: Thu Mar 05, 2009 4:59 pm
Location: Germany
Contact:

Re: Fatal Error Unable to allocate shared memory segment 128

Post by tedtiger » Wed Oct 16, 2013 9:30 am

Could be a defective RAM.

will3390
Posts: 6
Joined: Mon Oct 07, 2013 12:04 pm

Re: Fatal Error Unable to allocate shared memory segment 128

Post by will3390 » Wed Oct 16, 2013 6:19 pm

Is that something I can test myself? Or must my service provider test it? I'm on a dedicated virtual server. Likely, if RAM was defective, it would be affecting more than one customer.

will3390
Posts: 6
Joined: Mon Oct 07, 2013 12:04 pm

Re: Fatal Error Unable to allocate shared memory segment 128

Post by will3390 » Wed Oct 16, 2013 8:57 pm

Problem solved. It came down changing my server settings per domain to run PHP as an Apache Module rather than fastCGI. Apparently, fastCGI does not play well will PHP accelerators. Here is what happens:

— A 'php-cgi' process is spun up to fulfill a request(s)
— PHP opcode is cached
— The 'php-cgi' process dies and the cached data dies along with it
— It eventually leads to Internal Server Errors

Unfortunately, the cached data does not transfer to another 'php-cgi' process. So, the caching happens for each and every process, which is essentially useless. The way around this, without installing additional software and making other configuration changes, is to run PHP as an Apache Module.

Post Reply