New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mojo::IOLoop::Stream memory hog #1256
Comments
It is important to actually
Here, the buffer itself is still using a million bytes of RAM (the LEN field)
The old buffer has been returned to the malloc pool, and a new one created at only 10 bytes long. |
So we've added this change to hopefully address the problem. We haven't come to a good way to test it in the test suite, so for a first check @leonerd can you see if this at least is effective? |
Yeah, testing this kind of thing is always rather tricky. But the code looks to be doing the right thing on a visual inspection at least. I can deploy it and see how it behaves, though due to the rare nature of the problem in practice it may be a while before it turns up again. |
In that case I think I'll close this for now and please feel free to either reopen or open a new bug and reference this one if you should notice it in the future. Thanks again for the report. |
After `undef $self->{buffer}`, we can no longer call `length` on it in Perl 5.10 (later Perls more usefully treat `undef` as empty without warning). But, according to mojolicious#1256, we can't just set `$self->{buffer}` to the empty string without causing Perl to hold on to all the memory it allocated for our buffer. So, we have to first `undef`, then set to a defined value to prevent warnings from `length` on Perl 5.10.
After `undef $self->{buffer}`, we can no longer call `length` on it in Perl 5.10 (later Perls more usefully treat `undef` as empty without warning). But, according to mojolicious#1256, we can't just set `$self->{buffer}` to the empty string without causing Perl to hold on to all the memory it allocated for our buffer. So, we have to first `undef`, then set to a defined value to prevent warnings from `length` on Perl 5.10.
Steps to reproduce the behavior
The
{buffer}
field of aMojo::IOLoop::Stream
stores pending bytes for output. Once written, they aresubstr()
ed out of the buffer. This means that the memory usage of the actual buffer SV is never actually returned to the malloc pool.This has the problem that if e.g. a temporary network stall happens, large buffers are created that use up lots of memory, which perl never gets around to reusing even after the buffers are flushed.
Expected behavior
Once the underlying
_write
method has successfully flushed the contents of the buffer, it should beundef
ed to return the actual byte buffer back to the malloc pool, so it can be reused.Actual behavior
Because currently it doesn't get
undef
ed, any allocated byte storage remains, consuming memory that the rest of the process can't use.The text was updated successfully, but these errors were encountered: