1. Home
  2. Docs
  3. Contribute
  4. Memory management

Memory management

NectarJS manage the memory in real time, without a garbage collector.

Integrated shared pointer

In NectarJS, each value is a pointer to a typed value object. Each value object has its own counter, that let them, when they are out of scope, to determine if they have to delete themselves or not.

For example: let’s take this code:

function setMessage(_envelope)
{ // _envelope.counter++
  _envelope.msg = "hello";
} // _envelope.counter--

var myEnvelope = {}; // myEnvelope.counter = 1

setMessage(myEnvelope);

console.log(myEnvelope.msg);

// myEnvelope.counter = 0;

In this piece of code, the function setMessage takes _envelope as argument and set the property msg with the message “hello”.

Our program, once executed, create a var myEnvelope as en empty object. NectarJS creates in this moment a var with a pointer to an Object value. This Object value has a counter of 1.

Then, we call the function setMessage, with myEnvelope has an argument. At this moment, myEnvelope is passed as a reference in the function setMessage, NectarJS increments the counter of the _envelop value, the same as myEnvelope, by 1. So, the counter of myEnvelope is 2.

We set the property msg of myEnvelope with the value “hello”, then we the end of setMessage. At this moment, _envelope is out of scope, so NectarJS decrements the counter of the _envelope value by 1. At this moment, the counter of the value of myEnvelope is 1, not inferior at 1, so NectarJS doesn’t delete it.

We then call the function console.log() with myEnvelope.msg as parameter. This will print “hello” in the console. Then, we reach the end of the program, so myEnvelope is out of scope. NectarJS decrements its value by 1. Its value is now 0, inferior at 1, so NectarJS deletes it.

Memory Register

A var in NectarJS has its own pointer, pointing to a location in the memory register. When a variable is created, copied or passed by reference, a new pointer is always created to handle it. Once the var is out of scope, the pointer is marked as free to use : it means that the location in the register is now free to be reused.

For example, we have 6 variables: var_a, var_b, var_c, var_d, var_e, var_f. Each var is created and each var as a pointer value starting from 0 to 5. This is our register:

VAR_AVAR_BVAR_CVAR_DVAR_EVAR_F

In our code, we are going out of the scope of var_c, so the register location pointed by the var_c pointer is cleaned, our new register is:

VAR_AVAR_BEMPTYVAR_DVAR_EVAR_F

It means that, if we create a new var, named var_g, the memory manager will give 3 as the next pointer value for this new var. Our register will look like this:

VAR_AVAR_BVAR_GVAR_DVAR_EVAR_F

Why a memory register ?

The memory register has 2 bigs advantages than no register at all:

  • – Up to 10x performance gain
  • – Debugging features for the futur of NectarJS

It appears than on all the benchmarks we ran, we went from minutes to seconds for execution time with the memory register. Regarding the memory management and how the variables are handled, the register is also a great tool to help us to investigate problems like memory leaks.

Was this article helpful to you? Yes No

How can we help?