In Node.js, when is the data stored on the heap?

In C, you explicitly request and manage memory on the heap, so the interaction with the heap is clearly defined / obvious. How do you talk about this in Node.js?

sub questions:

  • where / how are functions stored?
  • Are there certain objects / primitives that are always stored on the heap? (e.g. buffers).
  • Are data transferred from the stack to the heap? when?

Links to good resources on this subject will also be appreciated, thanks.

+6
source share
1 answer

You do not care about the stack against the heap and about freeing up memory. This happens automatically, as Node.js offers an accurate trash garbage collector. Some data is stored in the GC heap. Some data is on the stack. You cannot say at all, because it depends on optimizations performed by the JIT compiler at runtime. Profiling tools can provide permeability to applications.

For non-memory resources (like files and sockets), use finally :

 var file = open(…); try { … } finally { close(file); } 
0
source

All Articles