Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup module.children after evaluating #9

Merged
merged 1 commit into from
Sep 16, 2017
Merged

Cleanup module.children after evaluating #9

merged 1 commit into from
Sep 16, 2017

Conversation

SevInf
Copy link
Contributor

@SevInf SevInf commented Sep 15, 2017

I've stumbled upon this while trying to debug a memory leak in postcss-loader. It uses cosmiconfig which in turn uses require-from-string to load javascript config. The problem is, Module constructor adds newly created module to parent.children which prevents evaluated values from ever getting garbage collected.

Without this, required-from-string module never gets garbage collected,
even when exports value is no longer used.
@sudo-suhas
Copy link
Contributor

@SevInf Instead of cleaning up, would it be possible to leverage require.cache?

Here's a simple demo:

'use strict';

const fs = require('fs');
const path = require('path');
const requireFromString = require('require-from-string');

const file = path.join(__dirname, 'experiment.js');
const code = fs.readFileSync(file, 'utf8');
// var result =
requireFromString(code, file);

console.log(require.cache[__filename])

Output:

Module {
  id: '.',
  exports: {},
  parent: null,
  filename: 'E:\\Projects\\repos\\cosmiconfig\\req-str.js',
  loaded: false,
  children:
   [ Module {
       id: 'E:\\Projects\\repos\\cosmiconfig\\node_modules\\require-from-string\\index.js',
       exports: [Function: requireFromString],
       parent: [Object],
       filename: 'E:\\Projects\\repos\\cosmiconfig\\node_modules\\require-from-string\\index.js',
       loaded: true,
       children: [],
       paths: [Array] },
     Module {
       id: 'E:\\Projects\\repos\\cosmiconfig\\experiment.js',
       exports: 1,
       parent: [Object],
       filename: 'E:\\Projects\\repos\\cosmiconfig\\experiment.js',
       loaded: false,
       children: [],
       paths: [Array] } ],
  paths:
   [ 'E:\\Projects\\repos\\cosmiconfig\\node_modules',
     'E:\\Projects\\repos\\node_modules',
     'E:\\Projects\\node_modules',
     'E:\\node_modules' ] }

@floatdrop
Copy link
Owner

@SevInf agreed, module parents should be cleaned up and gc should only keep exports if they are around.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants