Skip to content
This repository was archived by the owner on Sep 18, 2024. It is now read-only.

Updated deleting required files to fix memory leak#46

Merged
YorkAARGH merged 1 commit intoAnIdiotsGuide:masterfrom
dowzhong:patch-1
Jun 11, 2018
Merged

Updated deleting required files to fix memory leak#46
YorkAARGH merged 1 commit intoAnIdiotsGuide:masterfrom
dowzhong:patch-1

Conversation

@dowzhong
Copy link
Contributor

@dowzhong dowzhong commented May 23, 2018

Please describe the changes this PR makes and why it should be merged, as well as any and all proof of successful tests:
Simply delete require.cache[module] results in a memory leak after some time as the deleted module is still referenced in require.cache[module].parent.children, preventing it from GCing. The new function will allow it to GC properly.

Here is the previous unchanged file from chrome devtools:
https://i.imgur.com/59GlEja.png

After change, all the modules disappear after GC.

Semantic versioning classification:

  • This PR changes the framework's interface (methods or parameters added)
    • This PR includes breaking changes (methods removed or renamed, parameters moved or removed)
  • This PR only includes non-code changes, like changes to README, etc.

Simply delete require.cache[module] results in a memory leak after some time as the deleted module is still referenced in require.cache[module].parent.children, preventing it from GCing. The new function will allow it to GC properly.
@kyranet kyranet self-assigned this May 23, 2018
Copy link

@kyranet kyranet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can reproduce this issue, good catch! I was unaware of this reloading issue.

client.on(eventName, event.bind(null, client));
const mod = require.cache[require.resolve(`./events/${file}`)];
delete require.cache[require.resolve(`./events/${file}`)];
for (let i = 0; i < mod.parent.children.length; i++) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use this instead:

const index = mod.parent.children.indexOf(mod);
if (index !== -1) mod.parent.children.splice(index, 1);

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants