Explain in detail about the memory life cycle of JavaScript?

JavascriptObject Oriented ProgrammingProgramming

Memory cycle

Regardless of the programming language, the memory cycle is almost same for any programming language.

 There are 3 steps in a memory life cycle

1) Allocation of memory .

2) use the allocated memory(reading or writing)

3) Release the allocated memory when it is unnecessary.

The first and last parts are directly connected in low-level languages but are indirectly connected in high-level languages such as JavaScript.

1) Allocation of memory in javascript

JavaScript is called garbage collected language, that is when variables are declared, it will automatically allocate memory to them.When there are no more references for the declared variables, allocated memory will be released. 

Example

In the following example javascript allocated memory for a number, a string and an object.

var n = 989;      // allocates memory for a number
var s = 'qwerty'; // allocates memory for a string

var o = {
        a: 1,
        b: null
        };        // allocates memory for an object and contained values

2) Using the allocated values

Using values basically means reading and writing in allocated memory.This can be done by reading or writing the         value of a variable or an object property or even passing an argument to a function.

3) Release the allocated memory when it unnecessary

Most of the memory management issues will come in this stage.The herculean task here is to figure out when the         allocated memory is not needed any longer.To sort out this issue most of the high level languages embed a piece of   software called  garbage collector.

The task of garbage collector is to track memory allocation and find when the allocated memory is of no longer           required so as to release it.Unfortunately this process is just an estimation because the general problem of knowing whether some piece of memory is needed is undecidable.(algorithm can't trace out) 

Javascript garbage collector uses some algorithms such as Reference-counting garbage collection to figure out the memory which is no longer in use. 

raja
Published on 30-May-2019 20:26:12
Advertisements