-
Notifications
You must be signed in to change notification settings - Fork 7.3k
fs.createWriteStream memory leak #8048
Comments
Please also note that the problem seems to occur on node 10.29 and 10.30. |
I created a simple replacement for fs.createWriteStream that uses stream buffers that helps work around my issue but I still think something is up with fs.createWriteStream. |
Sorry, until you can either cause the Node process to consume more than 1.5GB of memory or cause a Do you have a test case that causes either of the above to occur? |
After some trying I haven't been able to make node get get an out of memory error or use more than 1.5GB of ram with these simple tests . So I the topic of this issue probably should be changed to "fs.createWriteStream very slow when lots of writes". |
Looking into this more... It turns out fs.createWriteStream dies if you don't respect the return value of stream.write. See here: http://nodejs.org/api/stream.html#stream_event_drain I still believe there is a problem with what node does when you don't handle this case but the workaround is that you must detect this condition and listen for the drain event to continue writing. I have updated my test-case @ https://github.com/ashelley/node-create-stream-test that shows this working. Try running the tests on master (the broken version) then try running the tests on the fixed branch to see the difference when you listen to the drain event. I have updated the readme on the test repository with these instructions. The documentation on this behaviour might need to be updated to document the bad things that might happen when you don't respect this return value. It may be a bit misleading to say that this return value is simply advisory rather than indicating that you must take alternative actions before writing to the stream again.
It may be possible that on other operating systems this code works well. I've only tested on ubuntu 13.10 64 bit and ubuntu 12.04 64bit. |
Just an update on this. I think there is a problem in the underlying writable stream. I've created another test suite that shows that the zlib stream has the exact same problem. https://github.com/ashelley/node-zlib-stream-test I've made this test suite slightly easier to see working vs none working by not doing it on a separate branch. just simply modify test.js allowDrain = true to see how the stream works when you allow it to drain properly. This thread is marked tagged fs but it's starting to look like a generic writable stream problem. I have yet to confirm that anyone else is having these issues so i'm going to try to determine the underlying problem. Right now this is just tested on 10.29 but I assume 10.30 will have the same problem as it seems to be the same problem as with the fs.createWriteStream problem. Obviously any in sight is appreciated. |
Okay.... I just compiled node from http://nodejs.org/dist/v0.10.30/node-v0.10.30.tar.gz on centos 6.5:
And i'm having the same issue. |
Just compiled 0.11.3 from github
same issue so far |
Still seeing the same problem in 0.10.0 going to start looking at my testing methodology to make sure that it's sane :(
|
@trevnorris I believe this thread is now a dupe of #6623 |
i just checked out master... the problem still exists:
|
You're correct. It is a dup. There's an issue in the Stream API and how we always immediately call the callback in specific cases. When that happens memory begins to fill until the write is complete. Thanks for looking into this more. Going to close this. |
I checked out node 0.10 and started doing some diagnostics. After reviewing the code I feel as if draining WriteReq's after calling .end should have similar throughput as if you originally were writing chunks to the stream. It does not. Here is a patch against v0.10.0 that i'm using to try to visualize the problem: https://gist.github.com/ashelley/1aff66c7a45011343cd1 It seems to me that based on the information output from these diagnostics that something is slowing down between doWrite and onwriteStateUpdate. I haven't yet determined why this is happening and I still need to verify that my diagnostics are accurate but so far it looks like the the calls to onwrite are delayed subtly (by something) which causes this problem. Here is some output from my zlib tests:
here is some output from my fs tests:
|
Hello,
I'm having major problems with fs.createWriteStream in my code. It is seemingly leaking memory.
I have created a test case here:
https://github.com/ashelley/node-create-stream-test
clone the repository and then
There are three tests in:
https://github.com/ashelley/node-create-stream-test/blob/master/tests/tests.js
The first one writes out 200KB always calling setImmediate to allow the event loop to be relieved frequently. This test is able to output 200KB albeit very slow.
The second test calls setImmediate only every 100 iterations. This test shows the problems I'm having with fs.createWriteStream. You can see it really start to churn trying to generate the stream and also flush the data to the file.
I've added memwatch to the output of this test to show the leaks occurring.
The third test is synchronous and doesn't give v8 any time to breath. You can really see the problem here.
If you go into tests.js and change 200KB to something like 50KB all the tests will complete and everything will be fine.
look for a variable called fileSize in tests\tests.js to adjust the settings.
Is there anything I am doing wrong or is this a bug? Any insight is appreciated.... Thank you.
The text was updated successfully, but these errors were encountered: