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.

JS config never gets garbage collected.

See original GitHub issue

I discovered this issue while trying to catch a memory leak in our build tool. The way cosmiconfig loads ‘.js’ config prevents it from ever getting garbage collected, even if userland code no longer needs. require-from-string package creates a new Module record which then permanently stored in memory. You can see it with this sample:

const cosmiconfig = require('cosmiconfig');

const loadRC = require.resolve('cosmiconfig/lib/loadRc.js');

function loadOnce() {
    const explorer = cosmiconfig('example');
    return explorer.load(__dirname)
        .then(() => console.log(require.cache[loadRC].children.length))
}

let count = 1000;
function loop() {
    if (count > 0) {
        count--;
        loadOnce().then(loop);
    }
}

loop();

Provided there is a non-empty example.config.js nearby, you should see that require.cache[loadRC].children.length grows by 1 after every load and never gets cleaned up.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
sudo-suhascommented, Sep 17, 2017

Fixed and released in 3.0.1 🎉

1reaction
SevInfcommented, Sep 15, 2017

Ok, so far I’ve submitted a patch to require-from-string, doing the cleanup: floatdrop/require-from-string#9.

I’m still not entirely sure it belongs there, let’s see what maintainers say. But if it won’t be merged, I think we still can continue to use require-from-string and do this cleanup in cosmiconfig instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript and Garbage collection - Stack Overflow
To avoid garbage completely in JavaScript is very difficult, you could say impossible. Its depends, how you reuse the objects and variables to ......
Read more >
Memory management - JavaScript - MDN Web Docs
Some high-level languages, such as JavaScript, utilize a form of automatic memory management known as garbage collection (GC).
Read more >
Node.js Garbage Collection Explained - Blog - RisingStack
Learn how Node.js garbage collection and memory management works in practice. Code-level explanation and garbage collection examples inside.
Read more >
Understanding Garbage Collection and Hunting Memory ...
A look into how Node.js manages memory with garbage collection.
Read more >
Hunting Memory Leakage in JavaScript Application
In JavaScript, as no interface is exposed to memory management, all the memory management is relinquished to the user. It is garbage collection....
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