Does output buffering in PHP require more resources?
Asked Answered
T

2

8

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

Tillio answered 1/10, 2009 at 9:45 Comment(0)
L
3

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.

Latakia answered 1/10, 2009 at 9:53 Comment(3)
i just read abit abt PHP output buffer also. i am thinking it will require server memory to store the whole page b4 sending it out as 1 piece right? so there maybe a chance it will use more memory than without buffering?Humming
@Humming that was my initial concern and what I would really like to find outTillio
Sorry I forgot to mention this point... I think the overhead of output buffering is important when using gzhandler for compression. For simple buffering, it does store everything in memory, but I don't think you will see a huge difference (big html pages don't go over 200K, do they ?). You can easily test that with memory_get_usage() with and without output buffering.Latakia
L
5

There are two reasons why output buffering is useful

  1. For performance so you're not waiting for the network socket to be available each time you echo.
  2. To avoid sending the headers too early. Once you've sent some content to the browser the headers must also be sent, after this is done then you cannot modify them e.g. if you want to set a cookie or change the content-type.

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.

Limpet answered 1/10, 2009 at 11:50 Comment(0)
L
3

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.

Latakia answered 1/10, 2009 at 9:53 Comment(3)
i just read abit abt PHP output buffer also. i am thinking it will require server memory to store the whole page b4 sending it out as 1 piece right? so there maybe a chance it will use more memory than without buffering?Humming
@Humming that was my initial concern and what I would really like to find outTillio
Sorry I forgot to mention this point... I think the overhead of output buffering is important when using gzhandler for compression. For simple buffering, it does store everything in memory, but I don't think you will see a huge difference (big html pages don't go over 200K, do they ?). You can easily test that with memory_get_usage() with and without output buffering.Latakia

© 2022 - 2024 — McMap. All rights reserved.