-
Notifications
You must be signed in to change notification settings - Fork 152
Huge memory usage, despite using SapiStreamEmitter #232
Comments
First, for anyone reading this, you need to apply PR #233 before running this code. @sasezaki your suggestion will not work. However, may be interesting to add an "ob_end_flush" at the end of "listen" method (not ob_end_clean). In order to reduce memory usage is necessary a call to "ob_flush" after each "read" call in SapiStreamEmitter.php. However I believe this may cause performance issues in most production environments. The flush calls has a very high performance cost. And besides this, the PHP engine already regularly flushes output buffer, following "output_buffering" configuration directive. Which I believe is more appropriate because it allows the application admin to choose the best balance between resource consumption and performance. And depending of layers beyond PHP engine, the "ob_flush" may be pointless (e.g. Apache buffering when using gzip). |
It has been months (literally) since I started looking for a solution. In my case, here is what did the trick - Note that I'm only using the SapiStreamEmitter: ob_end_flush();
ob_implicit_flush();
$emitter->emit($response); I'm not sure if this is really efficient, but at least it works (with large files ofc). I still need to try it in prod though.. Not taking any credit here. In fact, this is from an answer (that have some up votes) on EDIT - After some more investigation.. We can simply do this. Way more cleaner. ob_implicit_flush();
$maxBufferLevel = 0; // Stands for the maximum (and last) output buffering level to unwrap
$emitter->emit($response, $maxBufferLevel); |
This repository has been closed and moved to laminas/laminas-diactoros; a new issue has been opened at laminas/laminas-diactoros#14. |
Huge memory usage, despite using SapiStreamEmitter
When using Zend\Diactoros\Server with
->setEmitter(new \Zend\Diactoros\Response\SapiStreamEmitter());
, it is still huge memory usage.here is reproduce code.
expected Memory Usage is
2.00 MB
,but showned Memory Usage is
16.00 MB
.I think that reason is Server::listen() calls
ob_start
,but did not clean output buffer (ob_end_clean)
The text was updated successfully, but these errors were encountered: