Git Product home page Git Product logo

Comments (3)

Richienb avatar Richienb commented on May 22, 2024

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.

Codex- avatar Codex- commented on May 22, 2024

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.

Richienb avatar Richienb commented on May 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.