JavaScript

Last Updated: 12/14/2022

Garbage Collection

  • In low level languages like C or C++, when you create an object, you need to allocate memory to it, and when we're done you need to reallocate memory.
  • In JavaScript, you don't have this concept.
  • You can easily create a new object, at the time you initialized this object, the memory is automatically allocated to this object, next you can use that, and when you are done using, you don't need to deallocate the memory.
  • JavaScript engine has a garbage collector. The job of this garbage collector is to find the variables or constants that are no longer used and then deallocate the memory that was allocated to them earlier.
  • You do not have to worry about this.
  • Memory allocation and deallocation automatically happens behind the scenes, and you have no control over that. You cannot tell garbage collector when to run and what variables to remove from memory.
  • Based on some complex algorithms, this garbage collector runs in the background.