Apache crashes due to PHP memory error

Apache crashes due to PHP memory error

by Jade Tippett -
Number of replies: 14
Moved from the Windows-based Servers forum at the suggestion of a respondent:

I am running Moodle 1.9.2 on an older dual Pentium III Dell server with 1.5 GB of memory in a single school with at most several hundred students using Moodle at the same time.

The underlying server architecture is the XAMPP package consisting of Apache 2.2.3, PHP 5.2.1.1, and MySQL (5.? I assume)

When too many people initiate transactions at once, Apache crashes. I have Apache configured to restart, but the drop-out is about 3 minutes long.

Apache's error.log contains the following entry:

Error in my_thread_global_end(): 254 threads didn't exit
zend_mm_heap corrupted
[Tue Jan 27 10:17:11 2009] [notice] Parent: child process exited with status 1 -- Restarting.

PHP's error log reads:

[27-Jan-2009 10:17:04] PHP Fatal error: Out of memory (allocated 11796480) (tried to allocate 1048577 bytes) in W:\webroot\moodle\lib\filelib.php on line 974
[27-Jan-2009 10:17:04] PHP Fatal error: Out of memory (allocated 6029312) (tried to allocate 24576 bytes) in W:\webroot\moodle\lib\accesslib.php on line 233
[27-Jan-2009 10:17:04] PHP Fatal error: Out of memory (allocated 4718592) (tried to allocate 24576 bytes) in W:\webroot\moodle\lib\weblib.php on line 6822
[27-Jan-2009 10:17:05] PHP Fatal error: Out of memory (allocated 4456448) (tried to allocate 98304 bytes) in W:\webroot\moodle\lib\weblib.php on line 5394
[27-Jan-2009 10:17:05] PHP Fatal error: Out of memory (allocated 3407872) (tried to allocate 24576 bytes) in W:\webroot\moodle\lib\weblib.php on line 897

php.ini is configured:

;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;

max_execution_time = 120 ; Maximum execution time of each script, in seconds
max_input_time = 60 ; Maximum amount of time each script may spend parsing request data
memory_limit = 32M ; Maximum amount of memory a script may consume (16MB)

I know the server is old, and the XAMPP package could be updated, but is there any way to configure Apache and PHP to avoid the crashes??

Thanks in advance for your help.

--j
Average of ratings: -
In reply to Jade Tippett

Re: Apache crashes due to PHP memory error

by Paul Nijbakker -
Hi Jade,

We experienced this problem caused by an Apache memory leak. See http://moodle.org/mod/forum/discuss.php?d=79359.
You can lower the load on the server by setting KeepAliveTimeout to something like 2.

Restarting the server every morning frees up RAM space and may keep you going.

Rgrds,
Paul.
In reply to Jade Tippett

Re: Apache crashes due to PHP memory error

by Martín Langhoff -
I seriously doubt it's a memory leak in the strict sense of the term. Apache's memory allocation strategies are odd, but it's not really a leak.

Search this forum for 'maxclients', 'maxrequestsperchild', 'keepalivetimeout' and 'ps_mem.py' -- those are the main tunables you need to touch.
In reply to Jade Tippett

Re: Apache crashes due to PHP memory error

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
You mean you are now *not* running on Windows?

memory_limit=32M

That's not enough. I hate to be all "RTFM" but it actually says so in the documentation somewhere. I would set that to 128M.(http://docs.moodle.org/en/Installing_Moodle#Recheck_PHP_settings)

But... there's more potential trouble. Several hundred students "at the same time" on 1.9 on that hardware is optimistic. Is there any way that you can (at least) upgrade the RAM?

The other thing is that you shouldn't be using XAMPP for a production server - it has many compromises and simply was never intended for this use. As most Linux distros give you a perfectly usable LAMP stack straight out of the tin, there's absolutely no need either.
In reply to Howard Miller

Re: Apache crashes due to PHP memory error

by Jade Tippett -
I appreciate the help. I'm just a classroom teacher with some--mostly self-taught--computer experience, so my sys-op skills are limited.

First, I adjusted the memory_limit to 128M. The rest of the Manual settings were already correct. (I had to look up "RTFM" thoughtful)

I left KeepAliveTimeout at 5. For the most part, this is an intranet Moodle inside our campus.

Does anyone have a simple Windows Script I could run daily using Task Scheduler to restart the Apache service? Windows Scripting is not among my skills.

As for a better server, updating Apache, PHP, MySQL, I think those would a smart. I am hoping that Obama's economic recovery funds for education may get us a new server. In the meantime, this is education in California. They keep telling me I'll get paid at the end of the month.

All of the work I do on this Moodle amounts to voluntary donations to my employer. I ration the amount of time I spend. Excuses...

Thank you again,

--j
In reply to Jade Tippett

Re: Apache crashes due to PHP memory error

by Martín Langhoff -
Regardless of your network setup, it's a good idea to reduce the KeepAliveTimeOut to 1. On a fast LAN, you could completely _disable_ keepalives, but still the best option is to set it to 1.

Read my original post about KeepAliveTimeout for an explanation of why 1 is the best setting smile

And... memory_limit does not help much. It doesn't do what you thing it does, unfortuntely.
In reply to Howard Miller

Re: Apache crashes due to PHP memory error

by Martín Langhoff -

Hi Howard

memory_limit doesn't do what you think it does smile -- and moodle lifts it internally, otherwise it wouldn't work at all.

If memory_limit is set low, and you remove the bit of code that raises it, Moodle just fails to operate. There is no way in PHP to ask for a bit of memory and do something if it's denied. It just dies. (In theory, in C you ask for memory and you get a chance to handle the case where the request is refused. In practice, you need memory to handle that case, and if you're against the memory limit already... your fallback code dies anyway.)

So

  • developers set memory_limit high so that we don't give 500-error to end users
  • developers try to write code that doesn't soak up much memory
  • admins monitor how much mem it actually consumes, and set maxclients accordingly (and buy mor RAM!)
In reply to Martín Langhoff

Re: Apache crashes due to PHP memory error

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Martin,

I learned everything I know from you :lol:

I'm a bit confused though... so what causes the "tried to allocate xxxx and failed" error? In the "good old days" when the memory limit was 8MB this was the most common complaint on moodle.org and the advice was always to increase memory_limit.
In reply to Howard Miller

Re: Apache crashes due to PHP memory error

by Martín Langhoff -
Ah, you're right, maybe he does have code that exceeds the memory_limit that Moodle sets, which I just checked and is 96MB.

I read it as meaning that he was hitting _real_ memory limit from the OS. Under linux/unix this could be a physical memory limit or more likely ulimit ur max shmem.
In reply to Martín Langhoff

Re: Apache crashes due to PHP memory error

by Jade Tippett -
Yep, it's a memory leak alright!

I spent the day watching the Task Manager between lessons and helping students. Over a period of about 3 hours, the memory allocation rose as high as 300,000 kb.

I set MaxMemFree at 300 for the moment. I'm not sure whether that will do anything or not. I was not able to find any real documentation on MaxMemFree and nothing on free(). We'll see.

I also made a batch file to restart Apache.exe daily and added it to the Scheduled tasks.

In my research, I ran into a build of Apache from Apache Lounge. It seems different in that it uses a lot of .h and .so files, unlike the Apache.org build. It also requires installation of a Visual C++ Redistribution library. Apparently this version is built using Visual C++ 9.0, whereas the Apache.org version is built using Visual C++ 6.0...or something like that.

The authors who said they were using this version claimed to have conquered the memory leakage problem.

Any recent feedback on the Apache Lounge distribution??

--j
In reply to Jade Tippett

Re: Apache crashes due to PHP memory error

by Martín Langhoff -
"Yep, it's a memory leak alright!"

The memory handling of apache is such that it may look like a leak to you, but it is not.

The solution to your problem lies mainly in tweaking mainly maxrequestsperchild and maxclients. Setting an OS limit will protect your OS but Apache/PHP/Moodle will DoS itself.
In reply to Martín Langhoff

Re: Apache crashes due to PHP memory error

by Jade Tippett -
This apache installation is running under Windows2003.

As I understand it, in Windows, apache starts one parent process and onechild process. This child process starts the specified number of threads, in my case 250 threads, which persist until they either crash or apache is stopped.

Specifying a MaxRequestsPerChild causes the child process to stop after the specified number of threads. Eventually apache creates another child process.

To me, this seems very similar to to restarting apache. I would rather restart apache when I know it will be idle.

Apache will not start with MaxClients specified. I get an Error 1.

I am still interested in MaxMemFree, if anyone has worked with this directive.

Onward!

--j
In reply to Jade Tippett

Re: Apache crashes due to PHP memory error

by Red Morris -

You aren't getting the same errors as I did, but it might be worth reading this post on my previous problems. http://moodle.org/mod/forum/discuss.php?d=110444

In the end I installed the latest versions and it all went away, but as I say in the post, it might have been the config twinkering too. There's a helpful link on that post to guide you through some of it.

In reply to Jade Tippett

Re: Apache crashes due to PHP memory error

by Jade Tippett -
It appears that MaxMemFree only works on the parent process. (If it affects anything at all.) I set MaxMemFree to 30 and it had no effect on the growth of memory usage by the child process. the parent process remained at about 20 mb throughout the day.