ob_gzhandler should not be used to compress big output as it cuases apache to fail?
25 Aug
Not having any inclanation to look into the PHP and APACHE source code I will have to qualify the statement in the title that it is coming from an observation and should not be treated as a consistent fact.
To make a long story short, I have moved from a shared hosting environment into an unmanaged VPS, which have a relativity small amount of RAM memory – 256M. One of the things that I am running on this VPS is a gregarious based RSS aggregator (pity that this project is virtually dead), the rest were my hebrew wordpress based blog, this wordpress mu based installation, and a mail server.
Once in several days the swap usage of the server will sky rocket and I will have to manually reboot the VPS. Though I have tried to find a common reason to this phenomenon, I could not fins one and I assumed That I am just over using my memory.
I have tried to do various change to the apache parameter controling the number of instances and the time it takes before they restart themselves, but altough there was an improvment, the server will still freeze from time to time.
One day the aggregator stopped giving any output at all. I have made some debugging and found out that what was killing it was an output compression done by calling ob_start(‘ob_gzhandler’) since when I have removed the call everything started working fine.
Then I have spent some time thinking about the “why” since while the prices of bandwidth have fallen through the years it is still nice to save if it is not too difficult. Finally it struck me that the only way the ob_gzhandler handler can work is by allocating memory which is in some way proportional to the amount of output generated, and for a memory starved machine and large output this can be a real killer since, as every software developer should know, there is not a single software on earth which can gracefully deal with shortage of memory resources.
Anyway, since most major web servers include now the capability of compressing the output, maybe it is better to leave it to them instead of implementing it in software, as they mught have a better dedicated code then PHP, and when they perform the compression they have probably cleaned the memory used by the PHP code, so they have more resources.
Wait a moment!, shouldn’t this line of reasoning be applicable to the usage of ob_start in any case? IMHO the answer is yes, and ob_start should be used for the shortest time, and the shortest output you can reasonably manage to get, unless you have idle memory and you don’t want it to feel neglected.