0

i am a newbie to designing a server in node js

i know that node js runs in a single process,

my doubts are

1)Is the memory state of local variables of a function stored even after its execution - Ex:function may start some asynchronous method whose callback is using those variables even after the function got executed.

So assuming the above was true

2)everytime the (server url) is called its callback is called with request,response variables.So the memory state must be continuosly increasing(cause every url call's memory state is stored i.e because the state of variables in the callback onRequest(request,response) are stored) and therefore it should crash at some point of time due to lack of memory.Does this happen?

1 Answers1

2

Is the memory state of local variables of a function stored even after its execution

Yes. This is called a closure. As long as your callback is active, the variables in its scope will be available.

everytime the (server url) is called its callback is called with request,response variables.So the memory state must be continuosly increasing and therefore it should crash at some point of time due to lack of memory.

No. When all the work is done, and no outstanding (asynchronous) callbacks do reference your variables any more, they will be garbage collected.

Does this happen?

Yes, you can write bad code that leads to memory leaks.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375