JS config never gets garbage collected.
See original GitHub issueI 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:
- Created 6 years ago
- Reactions:1
- Comments:11 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Fixed and released in 3.0.1 🎉
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 incosmiconfig
instead.