0

I have few questions related to memory usage in nodejs

  1. In the MVC project structure, can require file increase memory uses over time? eg :

    In server.js (main file i.e entry point of server)

    let user = require("./routes/user-r.js") //1kb during initial run,will it increase in size as number of hit increase?
    app.use("/",user);
    

    In routes/user-r.js

    let user_c = new (require("../controller/user-c.js")) //1kb during initial run,will it increase in size as a number of hit increase?
    module.export = (app) =>{
    
     // require above or inside below code does it make any difference in memory usage perspective?
     //  let user_c = new (require("../controller/user-c.js")) 
    
       route.get("/", user_c.Test())
    }
    
  2. In controller creating variable clear automatically after it returns response? eg:

    In controller/user-c.js

    let path = require("path") // will this increase memory size, every time when this controller call from routes
    class User {
       constructor(){
          this.user_m = new (require("./user-m.js") // is constructor remove this file from memory when not in use? i.e garbage collection remove this variable from memory
       }
       Test(){
           let x = "this is testing for memory" // is this variable clear from memeory every time?
       }
    

    }

  3. when garbage collection runs automatically? i.e after a certain default memory limit or after a response return by the controller

  4. what happens if the server has continuously hit in the 10-sec interval will garbage collection runs automatically?

  5. requiring the same file in a different variable can use twice memory size or same eg :

    let t = require("path"); let c = require("path"); is both variable consume memory ?

  6. suppose 3 controller files, consider as user-c, management-c , test-c. all these file requiring path ( require("path")) and storing it into 3 different variable. will it increase memory uses 3 times?

  7. is it possible to make a test file that can check for memory leakage?

  8. is there any npm packages that can check for memory usage?

bipin
  • 421
  • 8
  • 21
  • Does this answer your question? [Understanding Node.js modules: multiple requires return the same object?](https://stackoverflow.com/questions/8887318/understanding-node-js-modules-multiple-requires-return-the-same-object) Node's `require()` system does not, usually, reload objects when you require them more than once. – O. Jones May 13 '21 at 12:13
  • @O.Jones thanks for your replace but provided the link does not clear all my point – bipin May 13 '21 at 12:26

0 Answers0