Linux and Open Source Blog

  • Home
  • WordPress Plugins
  • About
  • Contact

HOWTO: PHP Output Buffering Example

Posted on December 12, 2011 by Linewbie.com Posted in PHP, Programming .

Though profiling our application I found a a quite strange memory usage because of the ob_start()#) functionality. We do work with ob_start()#) a reasonable amount to defer production of data, that is a common thing in most applications. What seemed to be unusual is of which 16 calls to help ob_start()#) in place chewing through pretty much 700kb of ram, given that the results being buffered not often exceeds 1-2kb, i thought this was quite unusual.

I started thinking about the C code on the ob_start()#) functionality and found this interesting chunk of code inside php_start_ob_buffer()

initial_size = 40*1024;
block_size = 10*1024;

Which directs PHP to help pre-allocate 40kb of data for every single ob_start()#) call and once this proves for being insufficient, increase by 10kb everytime. Ouch!

PHP does assist you to say how considerably memory ob_start()#) incorporate the use of, via 2nd parameter towards function. However, when you exceed that measurement, PHP will instantly flush the seized data to tv screen, which means that if you are really effective in predicting your buffer sizes or widely overestimate, there is a risk which the data will possibly be dumped to tv screen by PHP begin using this option.

Since i am not fabulous at guessing, I’ve decided to manufacture a small, backwards works tweak to PHP’s value that allow options of custom buffer sizes, but encourage the buffer size for being increased if the 1st buffer size proves for being insufficient, ensuring which the data can possibly be safely buffered. This functionality is implemented by using a change (view patch below) towards 1st parameter on the ob_start()#) functionality, which normally is needed to provide this callback function. While using the patch in area the parameter, generally is a number, which defines the required buffer size. While using the patch, ob_start(1024) shows that the 1kb buffer need to be used and only when it’s exceed keep allocating 1kb each time to allow intended for additional data for being stored. This solution does mean you should not use custom, resizable buffer sizes that has a callback function, but it surely does provider some sort of backwards (PHP API prudent) compatible strategy for implementing the efficiency in PHP 5. 3 and 5. 3.

Here is a simple before & after code sample:
PHP:

will print out 0, since buffer is over the limit and is flushed to output

PHP:

will print 1500, buffer was exceeded and then increased, making it possible for data to remain in the buffer

CODE:
Index: main/output.c
===================================================================
--- main/output.c (revision 320624)
+++ main/output.c (working copy)
@@ -155,10 +155,14 @@
initial_size = (chunk_size*3/2);
block_size = chunk_size/2;
} else {
- initial_size = 40*1024;
- block_size = 10*1024;

« How to force Skype to use Alsa on Linux – howto tip
WebOS will live on through open source firmware and software »

Leave a comment

Leave a comment Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Does an un-confirmed Bitcoin transaction expire?
  • Looting of the Fox: The Story of Sabotage at ShapeShift
  • Decentralization, Scalability, and Fault Tolerance of Bitcoin
  • Stripe will soon accept Bitcoin payments
  • Zynga announces Bitcoin acceptance in game
  • How to import very large sql dump via phpmyadmin
  • How to compare the content of two folders automatically
  • Top 5 reasons to start experimenting with Linux
  • The day our mind became open sourced
  • Mark Shuttleworth wants to turn canonical (ubuntu) into the next Apple Inc.

Categories

  • applications/software (26)
    • browsers (2)
    • development (1)
    • information management (1)
    • Mobility (1)
    • multimedia (5)
    • office suites (2)
    • security (6)
    • servers (6)
    • system (2)
  • audio/video/pics (3)
  • Bitcoin (3)
  • books & literature (1)
  • cms/portals (1)
  • desktop environments (7)
    • gnome (2)
    • kde (5)
  • events/shows (3)
    • interviews (1)
    • people (1)
    • surveys (1)
  • games & gaming (2)
  • general topics (4)
  • guides (112)
    • how to (105)
    • tips (87)
    • tutorials (86)
  • hardware (8)
    • desktop & laptop pc (5)
    • gadgets & mobiles (2)
  • howtoforge (47)
  • internet/web (4)
    • design & development (2)
  • linux and open source blog (49)
  • linux.com (76)
  • linux/unix/os distros (113)
    • debian/ubuntu based (10)
    • mac/osx (2)
    • other distros (3)
  • news (217)
  • open source (8)
    • business & foss (2)
  • other (26)
    • uncategorized (26)
  • Programming (3)
    • PHP (2)
  • quotes & thoughts (10)
  • random stuff (4)
    • cool stuff (3)
    • funny stuff (1)
  • review/preview/tests (7)
  • wordpress/blogging (3)

Archives

  • July 2016
  • April 2016
  • January 2015
  • April 2014
  • January 2014
  • November 2013
  • February 2013
  • November 2012
  • April 2012
  • March 2012
  • January 2012
  • December 2011
  • August 2011
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2006

Recent Posts

  • Does an un-confirmed Bitcoin transaction expire?
  • Looting of the Fox: The Story of Sabotage at ShapeShift
  • Decentralization, Scalability, and Fault Tolerance of Bitcoin
  • Stripe will soon accept Bitcoin payments
  • Zynga announces Bitcoin acceptance in game
  • How to import very large sql dump via phpmyadmin
  • How to compare the content of two folders automatically
  • Top 5 reasons to start experimenting with Linux
  • The day our mind became open sourced
  • Mark Shuttleworth wants to turn canonical (ubuntu) into the next Apple Inc.

Categories

  • applications/software (26)
    • browsers (2)
    • development (1)
    • information management (1)
    • Mobility (1)
    • multimedia (5)
    • office suites (2)
    • security (6)
    • servers (6)
    • system (2)
  • audio/video/pics (3)
  • Bitcoin (3)
  • books & literature (1)
  • cms/portals (1)
  • desktop environments (7)
    • gnome (2)
    • kde (5)
  • events/shows (3)
    • interviews (1)
    • people (1)
    • surveys (1)
  • games & gaming (2)
  • general topics (4)
  • guides (112)
    • how to (105)
    • tips (87)
    • tutorials (86)
  • hardware (8)
    • desktop & laptop pc (5)
    • gadgets & mobiles (2)
  • howtoforge (47)
  • internet/web (4)
    • design & development (2)
  • linux and open source blog (49)
  • linux.com (76)
  • linux/unix/os distros (113)
    • debian/ubuntu based (10)
    • mac/osx (2)
    • other distros (3)
  • news (217)
  • open source (8)
    • business & foss (2)
  • other (26)
    • uncategorized (26)
  • Programming (3)
    • PHP (2)
  • quotes & thoughts (10)
  • random stuff (4)
    • cool stuff (3)
    • funny stuff (1)
  • review/preview/tests (7)
  • wordpress/blogging (3)

Archives

  • July 2016
  • April 2016
  • January 2015
  • April 2014
  • January 2014
  • November 2013
  • February 2013
  • November 2012
  • April 2012
  • March 2012
  • January 2012
  • December 2011
  • August 2011
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2006
Privacy Policy

Est. 2002

linewbie.com serving the linux and open source community since April 09, 2002

CyberChimps WordPress Themes

© Linux and Open Source Blog