On Tue, Sep 24, 2019 at 2:26 AM Peter Stalman <sarke...@gmail.com> wrote:
> When PHP runs out of memory, a fatal error is triggered and whatever > shutdown > functions or error handlers take over. > > However, in the case of error logging, or just logging in general, there > often > needs to be additional memory used to accommodate the final logging > process. > > This can sort of be accomplished in userland a few ways: > > 1. Pre-allocating memory in a variable, such as the Yii2 error handler > (http://bit.ly/2kLnpd2), but this requires wasting memory on every > request. > > 2. Continuously checking the memory usage, but this increases code > complexity > needlessly and also wastes resources with constant checking. > > 3. A second process with its own memory allowance, but this also increases > complexity and transferring the required data for logging would require > serialization without using additional memory. I'm not sure how this > would be > accomplished. > > So I would like to suggest an option for setting a shutdown memory > allowance, > which would be the amount of additional memory allowed to be used by any > registered error handlers or shutdown functions. > > I think a C implementation of this in PHP would be far more efficient than > the > userland implementionations I mentioned. > Memory parachutes help when against a hard limit, but I'm not sure they're applicable in the soft limit scenario you describe. When the OOM condition fires, PHP resets its soft tracking of memory. You are free to allocate as much on the stack of the shutdown function, up to the soft limit again. Here[1] we see the shutdown function called because of OOM, then we allocate a local variable that consumes less than the soft limit (which is ok), while here[2] we see the shutdown function itself is constrained to the soft limit (it's killed b/c of OOM). Perhaps I am misunderstanding the scenario. Could you elaborate further, perhaps provide a concrete example demonstrating where a parachute would be needed? [1]:https://3v4l.org/bXMlX [2]:https://3v4l.org/HDkRc