question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Memory leak when just starting and stopping the server in jest test

See original GitHub issue

Versions

  • NodeJS: v14.18.1
  • mongodb-memory-server-*: 8.9.5
  • mongodb(the binary version): 0.0.0
  • mongodb(the js package): 4.4.0
  • system: MacOS

package: mongo-memory-server

What is the Problem?

Whenever I just start and stop the server I get a memory leak in my jest tests

Code Example

describe('testing, () => {
    let memorySever: MongoMemoryServer;

    beforeAll(async () => {
        memorySever = await MongoMemoryServer.create();
    });
    afterAll(async () => {
        await memorySever.stop();
        memorySever = undefined;
    });
    it("testing", async () => {

    });
});

Debug Output

Debug Output
  ● Test suite failed to run

    EXPERIMENTAL FEATURE!
    Your test suite is leaking memory. Please ensure all references are cleaned.

    There is a number of things that can leak memory:
      - Async operations that have not finished (e.g. fs.readFile).
      - Timers not properly mocked (e.g. setInterval, setTimeout).
      - Keeping references to the global scope.

      at onResult (../../node_modules/@jest/core/build/TestScheduler.js:190:18)
      at ../../node_modules/@jest/core/build/TestScheduler.js:316:17
      at ../../node_modules/emittery/index.js:260:13
          at Array.map (<anonymous>)
      at Emittery.emit (../../node_modules/emittery/index.js:258:23)

Do you know why it happenes?

no

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
hasezoeycommented, Nov 7, 2022

i have investigated this a bit, and from what i can tell, it seems because for each jest test file / suite, jest will create a new context (including loading all modules), but does not remove that context after the suite (file) is finished, but keeps it until the end, which means jest will load modules (including MMS) over and over (which also includes the cached data, but cannot be accessed from the new contexts), i have also no clue if this is intentional in jest or how to disable it.

Disclaimer: i am not proficient in searching for memory leaks, and this was actually my first time using the memory inspector

So my recommendation is: stick to the integration guide (using global setup and global teardown), unless you are using only one suite / test file.

i will close this for now, because OP has found a fix

0reactions
hasezoeycommented, Nov 8, 2022

i suspect MMS means mongodb-memory-server ?

yes

if jest caches the context, including the loaded modules … this includes MMS … you are talking about some cache, what cache exactly and can one clear it?

like i said, i have no clue how to clear or disable it, it seems like jest just does not remove the suite contexts after use, because they are still referenced by the individual test results, or i might be completely wrong - like i said i am not proficient in debugging with the memory inspector (it was my first use of it)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Your Jest Tests are Leaking Memory
Why does Jest leak memory? Jest resets the require cache between each test run. This is really nice because it gives test suites...
Read more >
[Bug]: Memory consumption issues on Node JS 16.11.0+ ...
Since Jest calls global.gc() when Garbage Collector is exposed and --logHeapUsage flag is present, the memory usage should be stable.
Read more >
Jest tests leaking due to improper teardown - Stack Overflow
This is likely caused by tests leaking due to improper teardown. Try running with --detectOpenHandles to find leaks." I realize that this is ......
Read more >
Finding the cause of a memory leak in Jest tests
Memory leaks can be nasty and pretty hard to find. We found that the hard way. We should always make sure to clean...
Read more >
Debugging Memory Leaks in Node.js Applications - Toptal
Memory leaks in long running Node.js applications are like ticking time bombs that, if left unchecked in production environments, can result in devastating ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found