Comments (3)
The queue
variable never goes out of scope so the garbage collector never removes it.
A better experiment would also manually invoke the garbage collector since it tends to be run whenever the engine wants it to. Try this with node --expose-gc
:
import PQueue from 'p-queue';
class Cow {
constructor(i) {
this.beef = i;
}
}
async function test(queue) {
for (let j = 0; j < 30_000; j++) {
if (j % 1000 === 0) {
console.log("Running:", j);
}
const promises = [];
for (let i = 0; i < 1000; i++) {
promises.push(
queue.add(async () => {
const array = [];
for (let i = 0; i < 100; i++) {
array.push(new Cow(i));
}
}),
);
}
await Promise.all(promises);
}
}
console.log("Before running:", process.memoryUsage());
await (async () => {
const queue = new PQueue({ concurrency: 5 });
await test(queue);
console.log('Completed');
})();
console.log("After running:", process.memoryUsage());
gc();
console.log("After garbage collection:", process.memoryUsage());
Output:
Before running: {
rss: 27095040,
heapTotal: 4915200,
heapUsed: 4509376,
external: 503134,
arrayBuffers: 70350
}
...
After running: {
rss: 62517248,
heapTotal: 41623552,
heapUsed: 20917184,
external: 469655,
arrayBuffers: 11166
}
After garbage collection: {
rss: 61730816,
heapTotal: 40574976,
heapUsed: 4136168,
external: 468417,
arrayBuffers: 10430
}
Notice how heapUsed
is 16 megabytes smaller after garbage collection then after running, somehow dropping even lower than before running.
from p-queue.
This doesn't really address the problem though, for an item that has been dequeued and no longer in the scope of the queue it should be dereferenced and gc'd, but in this case, it wasn't (I haven't retested since 3 years ago and ended up writing one for the use case lol)
Long-lived queues shouldn't need to be deleted and re-instantiated for their long dequeued items to be collected, seems like very undesirable behaviour.
For example, consider the case where a queue instance is used in a convenience class that provides some functionality that consumes items and queues them, then based on some criteria dequeues. At the end of the queue, it seems undesireable to then manage creating a new instance of the queue each time?
from p-queue.
The original example intentionally holds on to every single queued item in an array and passes them all to Promise.all
. With every reference in use, no garbage collection can happen.
from p-queue.
Related Issues (20)
- Timeout option question
- '/node_modules/eventemitter3/index.js' does not provide an export named 'default' HOT 2
- Again '/node_modules/eventemitter3/index.js' does not provide an export named 'default' HOT 1
- TypeError: PQueue is not a constructor HOT 1
- [Feature request] Get number of tasks before one
- Jest detects open handles on queue.add()
- Default value for priority doesn't get applied
- How do I only add items up until the concurrency limit and then wait until a pending promise is fulfilled?
- `PQueue.on` does not exist HOT 7
- How can I use jest fake timers when testing code that use p-queue?
- EventEmitter import breaks TypeScript
- [7.4.0] Type regression when node16 module/moduleResolution
- Weakmap.get is not a function HOT 7
- [react-native] privateMap.get is not a function HOT 3
- p-queue is not correctly exported and breaks Bundlephobia and unpkg. HOT 2
- AsyncLocalStorage execution context not propagated correctly
- TypeScript TS2344 error with enabled "exactOptionalPropertyTypes" setting.
- internal `pending` count out of sync
- Documentation: I feel I have a use-case that I'm not sure if the queue fulfils when reading the doc
- Feature Request - awaitNextInterval HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from p-queue.