Skip to content

Latest commit

 

History

History
131 lines (106 loc) · 4.87 KB

Memory management.md

File metadata and controls

131 lines (106 loc) · 4.87 KB

Memory management

Table of contents:

  1. Introduction.
  2. Memory Life Cycle.
  3. Garbage Collection.
  4. Reference Counting Garbage Collection algorithm.
  5. Mark and sweep algorithm.

Introduction

  • Low-level language (eg. C language) have low level memory management primitives like free(). ( this is not important to us now but we mention that to say that memory management must be done explicitly).
  • Unlike JavaScript, when for example objects are not used any more they are automatically "garbage collected" to free memory.
  • This gives a wrong impression to JS developers to not care about memory management.

Memory Life Cycle

  • Regardless of the programming language, memory life cycle is pretty much always the same:
    1. Allocate the memory you need. (explicit in low-level languages implicit in high-level languages like JS)
    2. Use the allocated memory. (read, write) (explicit in all languages)
    3. Release the allocated memory when it is not needed anymore. (explicit in low-level languages implicit in high-level languages like JS (GARBAGE COLLECTION))
Allocate the memory in JS

===> JavaScript does it alongside with declaring values.

Use the allocated memory

===> By reading or writing the value of a variable or an object property or even passing an argument to a function.

Release the allocated memory

===>

  • High-level languages embed a piece of software called "garbage collector" whose job is to track memory allocation and use in order to find when a piece of allocated memory is not needed any longer in which case, it will automatically free it.
  • This process is an approximation (knowing whether some piece of memory is needed is undecidable. This means it can not be solved by an algorithm).

Garbage Collection

  • The algorithms of garbage collection depends on "reference"

Reference Counting Garbage Collection Algorithm

  • Description: This is just an explanation of a garbage collection algorithm presented in MDN-Garbage Collection.
  • Definition: One of algorithms of JS Grabage Collection Algorithms.
  • The most naive Garbage Collection algorithm.
  • It reduces the definition of "an object is not needed anymore so Garbage collect it" to "an object has no other object referencing to it so Garbage collect it".
  • MDN Example with a more clear explanation:
var o = { 
  a: {
    b: 2
  }
}; 

2 objects are created:

{ 
  a: {
    b: 2
  }
}

And

{
    b: 2
  }

The first one is referenced by being assigned to variable o. The second is referenced by the first one as a property of it.

var o2 = o; 

Now the first object has a second reference o2

o = 1;     

But now one of its references o variable are being re assigned to a different value. So the first object now has one reference o2.

var oa = o2.a; 

The second object now has a reference oa by assigning the o2 object a property to oa

o2 = 'yo';

Now the first object loses its last reference o2. So it can be garbage collected.

oa = null; 

Now the second object loses its reference oa. So it can also be garbage collected.

  • Another example: trapped reference Or circularReference
var div;
window.onload = function() {
  div = document.getElementById('myDivElement');
  div.circularReference = div;// the div references itself. It will continue in memory even if it is removed from DOM tree.
  div.lotsOfData = new Array(10000).join('*');//the div carries a lot of data. Memory consumed by this data will never be released.
};

Mark and sweep algorithm

  • It reduces the definition of "an object is not needed anymore so Garbage collect it" to "an object is not reachable so Garbage collect it".
  • This algorithm starts from the root object ( global object) and find all objects that are referenced from these roots, then all objects referenced from these, etc. Then the unreachable objects will be garbage collected.
  • This algorithm is better than the previous one since "an object has zero references" leads to this object being unreachable. The opposite is not true as in this example:
function f() {
  var o = {};
  var o2 = {};
  o.a = o2; // o references o2
  o2.a = o; // o2 references o
  return 'hi';
}

f();