Skip to content

Memory Allocation

Memory allocation is the process of reserving a block of memory for a program to use. The memory allocation process is managed by the operating system and is responsible for allocating memory to processes and programs.

Implementation125

  • It stored in the computer's RAM.
  • Each thread has its own stack, and it is not shared between threads in multi-threaded applications.
  • In C++, each object created without the new keyword is stored in the stack.
  • Any stack memory allocated to a function is automatically deallocated when the function returns.
  • It has a limited size, if the stack memory is not enough space, it will lead to a stack overflow error. It normally happens when the recursive function is called infinitely.
  • It stored in the computer's RAM.
  • It is shared by all threads in multi-threaded applications.
  • In C++, objects created with the new keyword are stored in the heap. But we need to manually deallocate the memory when we are done with it else it will lead to memory leaks.
  • The heap memory will last until it is deallocated by the programmer or the program terminates.
  • If it has not enough space, operating system will allocate more memory to the heap.

Typescript/JavaScript

  • Javascript Runtime helps to manage the memory allocation and deallocation(via GC).
  • GC deallocates memory when they are no longer reachable or needed automatically3.
    1. Only incoming references are counted. If there is no incoming reference, the object is considered unreachable. Incoming references means the current object is holding by another object, or others are depending on it.4