aredridel / node-bin-gen

157 stars 51 forks source link

Memory leak - Base class instance #226

Closed hemendpatel closed 1 year ago

hemendpatel commented 1 year ago

I have noticed a memory issue with express:

I have create nodejs typescript app having get method which will create an object welcome to implement my business logic. I have run node --inspect and get see that the base class instance existing in memory after api request completed.

image

App.ts :

import express from 'express';
import { welcome } from './welcome';
const app = express();
const port = 3000;

app.get('/', (req, res) => {
    const screen: any = {}
    screen.test = new welcome();    
    res.send('Hello World!');
});

app.listen(port, () => {
    return console.log(`Express is listening at http://localhost:${port}`);
});

basecontroll.erts

export class BaseController {
    constructor() {
        console.log('BaseController');
    }
}

welcome.ts

import { BaseController } from "./basecontroller";

export class welcome extends BaseController {
    constructor() {
        super()
        console.log("welecome");

    }
}

I have tried to delete welcome object forcefully like delete screen.test but still baseclass instance existing in memory.

I have tried below command but we can't do this for each class

delete screen.test.__proto__.__proto__.constructor

This is working but it just work around.

Package.json:

{
  "name": "nodejsmemoryleak",
  "version": "1.0.0",
  "description": "",
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "scripts": {
    "start": "tsc && node --inspect dist/app.js",
    "build": "tsc"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/express": "^4.17.1",
    "nodemon": "^3.0.1",
    "ts-node": "^10.9.1",
    "typescript": "^5.1.6"
  },
  "dependencies": {
    "express": "^4.17.1"
  }
}
hemendpatel commented 1 year ago

Few more points:

In my case, All class inherit base class so I can see multiple base instance exist in memory for my application.

I have created welcome1 class which is also inheriting same BaseController. below is the memory snapshot after executing below code image

image

aredridel commented 1 year ago

This is interesting, but what does this have to do with this repository? This is a tool for publishing binary releases of node.js as npm packages.

I suspect you want the express or nodejs repos.

hemendpatel commented 1 year ago

Thank you @aredridel. I'm checking with express and NodeJS repos