When performance is important including server memory,I am curious if using output buffering like ob_start(); in PHP has ANY performance hits over not using it? Does it use more memory or anything to use it?
In my situation on a high traffic site where I need all the memory I can for memcache and APC and all the other server activities I am just curious if I should use it or not, the only real reason it comes in handy for me is for redirecting pages , sending headers I should say after a header has already been sent, my site has header , body, footer file setup so sometime I need to redirect depending on what is in the body file so if the header is already shown ion screen that creates a problem, using output buffering is 1 solution but there are other solutions so just curious about performance
Output Buffering is a method to tell the PHP engine to hold the output data before sending it to the browser.
An output buffer is a location in memory or cache where data ready to be seen is held until the display device is ready.
You can turn on output buffering with ob_start() , and turn it off with ob_end_flush() or ob_end_clean() . You can also have all your scripts automatically start with output buffering on using the output_buffering option in php.
You can enable output buffering for all files by setting this directive to 'On'. If you wish to limit the size of the buffer to a certain size - you can use a maximum number of bytes instead of 'On', as a value for this directive (e.g., output_buffering=4096). This directive is always Off in PHP-CLI.
There are two reasons why output buffering is useful
There is of course the penalty of storing everything in memory till the end of the request. This should, ordinarily, be quite small compared to the overall size of the PHP process. That is, unless, you plan on sending a massive file down the wire. If that's the case you can periodically flush the buffer using ob_flush() and flush() (or temporarily disable the buffer altogether) to reduce the peak memory used.
In my opinion you should have it on all the time and remove it in exceptional cases.
I think it's best to use it with a high traffic site, or at least turn implicit flush off, to avoid sending partial responses over network, because it can slow down the rest of the script if the receiver is very slow too.
By sending the whole response in one time, you free all resources used by the php script, so it's more efficient.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With