Memory leaking with Promise.map concurrency
See original GitHub issue-
What version of bluebird is the issue happening on? Bluebird 3.5.20 with bluebird-global 3.5.5
-
What platform and version? (For example Node.js 0.12 or Google Chrome 32) Node.js 8.9.
-
Did this issue happen with earlier version of bluebird? As far as I know
I have code that looks like this:
const aBunchOfIds = [ /* a few hundred thousand elements */ ]
Promise.map(aBunchOfIds, async id => {
const anObj = await getFromDatastore(id);
const anotherObj = await process(anObj);
if (isSpecial(anotherObj)) { console.log('so special'); }
}, { concurrency: 1000 });
This dies because node runs out of heap space. However the following modification resolves the issue:
const aBunchOfIds = [ /* a few hundred thousand elements */ ]
for (let i = 0; i < aBunchOfIds.length; i += 10000) {
const someIds = aBunchOfIds.slice(i, i + 10000);
Promise.map(someIds, async id => {
const anObj = await getFromDatastore(id);
const anotherObj = await process(anObj);
if (isSpecial(anotherObj)) { console.log('so special'); }
}, { concurrency: 1000 });
}
So it seems that Promise.map doesn’t free the memory used by its promises until all promises have returned, even when a concurrency is specified.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:6
- Comments:15 (1 by maintainers)
Top Results From Across the Web
How to resolve memory leak in node.js due to .map function
I am running into a memory issue which I suspect is due to my map functions.( Bluebird Promise.map). The below Myfucntion() calls the ......
Read more >Looping over await Promise.all memory leak : r/javascript
I run over 500 000 uids to fetch their associated data via a promise (promiseUserData(uid)). For some reason the memory usage is not...
Read more >The 4 Types of Memory Leaks in Node.js and How to Avoid ...
A look at memory leaks in caching and promises. a concrete building wall with an open window and ... Caching is the most...
Read more >Introduction - Alexandru Nedelcu
As you'll see, there are some reasonable arguments for why the Promise implementation is allowed to leak memory. But I can't agree.
Read more >Promise.map - Bluebird JS
Map Option: concurrency ... You may optionally specify a concurrency limit: ...map(..., {concurrency: 3});. The concurrency limit applies to Promises returned by ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m seeing memory-leak issues that are very similar, if not the same.
Came up with the following test-case which shows the issue clearly:
then()
added (see below) will allow GC to free up mem.Lodash 3.5.2
It seems that since the result isn’t passed in as argument to the last
then()
the object can be freed from mem. This seems to suggest that not only the promises themselves but all objects created inside those promises are kept locked in mem until the entirePromise.map
is finished.Just a heads up that I got “Maximum call stack size exceeded” errors until I finally downgraded this package to 3.5.2
I think it could be related to this issue.