menu
techminis

A naukri.com initiative

google-web-stories
Home

>

Programming News

>

How JavaSc...
source image

Medium

2M

read

206

img
dot

Image Credit: Medium

How JavaScript Handles Garbage Collection and Memory

  • JavaScript manages memory through the stack and heap. Primitives like numbers are stored on the stack, while objects go to the heap.
  • Function calls create stack frames that hold local variables and parameters, which are discarded once the function completes.
  • Memory usage grows as programs run, especially with dynamic data structures like arrays that reside in the heap.
  • Closures in JavaScript can retain memory even after the outer function finishes, influencing garbage collection.
  • Objects are shared by reference, and memory remains active until all references are released.
  • The garbage collector in JavaScript decides what data to clean up based on reachability from active parts of the program.
  • Modern engines like V8 use a generational garbage collector that marks and sweeps memory to reclaim unused objects.
  • References to variables, closures, and nested objects can prolong memory retention until they are no longer reachable.
  • Circular references are handled by modern engines to ensure proper clean-up when no external references remain.
  • Garbage collection in JavaScript is crucial for maintaining memory efficiency and program performance.

Read Full Article

like

12 Likes

For uninterrupted reading, download the app