Why does ob_get_clean wipe memory without starting the buffer with ob_start()?

Asked

Viewed 1,195 times

6

I was one day doing some tests, analyzing how functions influence memory usage, and, by chance, called the function ob_get_clean(). I noticed that the function call "decreased" the memory size used by php.

With ob_start() I noticed earlier that the use of memory increased, as expected, because it stores the output in a buffer.

Now I don’t understand why ob_get_clean() "cleaned" some value of buffer, decreasing memory, and ob_start() was not called anywhere in the code.

Below follows the results I obtained:

<?php
echo memory_get_usage(); //122224
?>

<?php
ob_get_clean();
echo memory_get_usage(); //113992
?>

Does anyone know why when calling ob_get_clean() memory has diminished?

Is there any configuration in php.ini that starts the buffer automatically or something like that?

  • 2

    The directive ob_implicit_flush is disabled? If yes, it may be that PHP is saving the output in memory to, only at the end, cause the buffer to be released from memory.

  • Would not be implicit_flush? I found this line in my php.ini implicit_flush = Off. Is disabled.

  • I just took the test by changing this directive to "On". I got different results, but when I use ob_end_clean continues to "decrease" memory usage. without ob_end_clean = 121808 and with ob_end_clean = 113576

1 answer

2


Yes, there is.

In his php.ini how the option is configured output_buffering ?

output_buffering=On

Activates the automatic output buffer.

You can disable it:

output_buffering=Off

or specify buffer size:

output_buffering=4096

See more: Output Buffering in the PHP manual

  • 1

    that’s just it! I ran the same tests here! In my php.ini, this option was marked as 4096. When trying to call ob_end_clean() I received an error: Notice: ob_end_clean(): failed to delete buffer. No buffer to delete in C:\xampp\htdocs\teste\index.php on line 2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.