Skip to content

Instantly share code, notes, and snippets.

@koonuf
Last active June 7, 2018 06:33
Show Gist options
  • Save koonuf/38c94a39c86a0c198999d93d8c0b66e3 to your computer and use it in GitHub Desktop.
Save koonuf/38c94a39c86a0c198999d93d8c0b66e3 to your computer and use it in GitHub Desktop.
var globalContainer = null;
var replaceGlobalContainer = function() {
var localRef = globalContainer;
var unused = function () {
// refering to localRef inside even unused function
// puts it into replaceGlobalContainer call Context
// it no longer is truly a "local" variable, it needs to
// be pinned to the heap
if (localRef) {
console.log("");
}
};
// we replace globalContainer reference by a new reference to a large object
// old reference should now be garbage-collectable...
globalContainer = {
largeObj: new Array(10000000).join("*"),
methodToRefContext: function () {
// ... however the new reference has a method refering to
// replaceGlobalContainer call Context, which refers to localRef
// and makes old reference live forever
console.log("");
}
};
console.log("Heap size: %sMb", Math.floor(process.memoryUsage().heapTotal / 1024 / 1024));
};
var runAndReschedule = function () {
replaceGlobalContainer();
setTimeout(runAndReschedule, 500);
};
console.log("Pid: %s", process.pid);
runAndReschedule();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment