Skip to content

Memory leak when creating a new Impit instance on each iteration (Node.js) #331

@rustairov

Description

@rustairov

Hi! I'm running into a memory-related issue when using the library.

I have a scenario where each iteration needs its own isolated cookie storage:
– the first request obtains cookies,
– subsequent requests in the same iteration must reuse those cookies,
– iterations must not share cookies with each other.

Here’s a minimal reproducible example:

for (let i = 0; i < 1e3; i++) {
  const cookieJar = new CookieJar();
  const impit = new Impit({ cookieJar });

  try {
    const resp = await impit.fetch('http://localhost:3000'); // succeeds or fails depending on server availability
    console.log(Object.values(process.memoryUsage()).join(','));

    // follow-up requests that rely on cookies from the first request
  } catch (err) {
    console.log(Object.values(process.memoryUsage()).join(','));
  }
}

The problem:
Creating a new Impit instance on every iteration causes memory usage to continuously grow until the process runs out of memory. It seems that Impit instances (or internal state) are not being released by GC even after the loop moves on.

My question:
What is the correct approach to using the library so that each iteration can perform several sequential requests with its own cookie jar, but without creating a memory leak?

Is there a recommended pattern for:

  • per-iteration cookie isolation,
  • reusing some internal resources instead of reinstantiating Impit thousands of times,
  • or explicitly cleaning up an Impit instance?

Thanks in advance!

Metadata

Metadata

Assignees

Labels

t-toolingIssues with this label are in the ownership of the tooling team.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions