Open psnider opened 8 years ago
@mcollina Any high level thoughts on this?
FWIW, here are some relevant node.js bugs:
Also, I changed generateLargeBuffer() to use 10% of the size, 4363201, and I still see the problem.
And changing generateLargeBuffer() to use a smaller size buffer of 4*1024, with enough calls to match the volume of memory of the preceding ~4MB test, yields similar results.
// 4k * 4k ~= 4 * 4MB
for (var i = 0 ; i < 4*1024 ; ++i) {
test()
}
So it doesn't appear to be related to the size.
And I have confirmed that when I rework my main program to make direct calls instead of using seneca for the one problematic message, memory management works as expected.
@mcollina @rjrodger Before I go spelunky on this one. Do either of ye have any comments on this?
does this also happen with seneca({actcache: {active: false}})
?
Let me try that and get back.
@psnider I suggest you to reconsider your architecture. Sending 40MB of data from Node.js will likely cause you problems in other places as well. The solution to this issue is uploading that file to S3 or another blob storage, and then send a URL to your caller. This has the added benefit of allowing better caching.
Given that there are no leaks for small strings, I presume it's some bad interaction with long strings and Seneca. There are quite a few _.clone
in Seneca, and I think those are the main culprit (if there is no actcache).
I agree that 40MB is too large, but I have no control over what the external service sends.
Given that there are no leaks for small strings
Are you suggesting that 4k is a long string? If so, what is a reasonable maximum length? Please reference: https://github.com/senecajs/seneca/issues/401#issuecomment-212223976
Receiving 40MB in your process for a live transaction is probably a bad idea anyway. You will get a much more performing user experience by 1) having the client return an original URL or 2) uploading that data somewhere else in a batch/queue fashion.
Regarding the bug, I expressed myself badly. Given there is no problem with "normal" strings and objects, this should be tightly correlated with buffers.
Also, why you need to convert it to a String? Keeping it as a buffer will definitely help in memory management.
I believe that there is a serious memory leak in this use case. I don't know if Seneca is involved, but it seems to be. If it is related to Seneca, it would help to know what the cause is.
BTW, I worked carefully on the examples I gave in the initial report to decouple it from all other aspects of my app.
@psnider Let me first check the cache stuff, if that's the issue it's fairly simple to fix. I'll report back soon.
I need to consider how to handle very large messages in terms of graceful degredation.
I've encountered a severe memory leak when an external service started sending very large XML responses (~40MB).
Note that there are some bug reports in node with very large strings leaking, but they must be around 250MB, and seem to be unrelated to this issue. Also , I couldn't reproduce this problem when I don't use seneca.
node v5.0.0 seneca ^1.3.0
Here is minimal code I use from a REPL that reproduces the issue reliably. Enable garbage collection API to help with testing:
node --expose-gc
Find the base heap size:
For me this gives: { rss: 52281344, heapTotal: 32966400, heapUsed: 26,996,888 }
Then excercise the above code from the REPL with combinations of tests and measurements and running the GC.
Wait for the tests to finish, then run:
For me this gives: { rss: 293875712, heapTotal: 152241920, heapUsed: 115,433,200 }
Verify that this memory can be reclaimed by running the GC:
For me this gives: { rss: 182329344, heapTotal: 31946496, heapUsed: 26,413,976 } So it appears that memory is reclaimed correctly.
Now switch to performing the toString() from within the plugin:
Again, wait for the tests to finish, then run:
For me this gives: { rss: 527720448, heapTotal: 206558208, heapUsed: 198,439,760 }
Now confirm that the GC cannot reclaim this memory any longer.
For me this gives: { rss: 523902976, heapTotal: 204523008, heapUsed: 198,208,656 }
In contrast to before, it has dropped only by about 200k !