Skip to end of metadata
Go to start of metadata

 

The goal of this project was to investigate memory management in Javascript.

 

Task 1:

Javascript generally does not have any explicit memory allocation or deallocation. It does have one tool to deallocate memory, although it mostly uses garbage collection. I will explain more about memory management in JavaScript using examples of memory allocation and deallocation:

 

These are examples of how to allocate memory.  The var symbol is used to when defining a variable for the first time, so it is the keyword used to allocate memory related to that variable.  As shown in the example, var can be used to allocate numbers, strings, objects, and lists in Javascript.  These are the most simple allocations.  The keyword function also allocates memory since functions are callable objects.  The details of the function are stored in that memory space.  The last two examples in the image above show that the result of calling a function or method can also allocate memory.  These are very similar to C assigns memory to variables.  The main difference between C and JavaScript is that C can allocate memory using malloc to a pointer.  There is no way to do this in JavaScript.

Then there is the issue of garbage collection in JavaScript.  JavaScript does garbage collection a little differently than Ruby: rather than stopping to garbage collect, garbage collection happens in parallel with the program being executed.  For example:

 

 

This creates an object that has another object in it. After it is make, it cannot be garbage collected because there is still a reference to it - it could be used in the future.  If new_obj gets big_object and then big_object is reassigned, the memory originally created for big_obj is not garbage collected because new obj now references it.  If another object gets the value of new object and new object is reassigned, there is now no reference to the original big obj object but there is a reference to something in it, so it cannot be garbage collected.  When sec obj is reassigned there is now nothing referencing big obj left, so it can never be accessed again, so it is garbage collected.  

Another example is:

In this case, the value of the first object points to the object of the second object and vice versa.  Therefore, even after the first and the second objects are reassigned, the values of the objects still have references to each other, so then by reference garbage collection, they will never be garbage collected.  JavaScript also uses mark and sweep garbage collection (where it checks to see if each item can be reached, and garbage collects everything that cannot).  This deals with this problem. The last two lines of this example show a way for the programmer explicitly deallocate memory.  This deletes the variable and deallocates the memory.  It can only be used on object members and global variables though. 

 

This example shows a memory management fact that is going to change the way that I program.  Notice that in the first function, global_var is not declared with a var.  In Javascript, this makes global_var a variable in the global scope; therefore, even when the function scope is done, the memory associated with them will not be garbage collected.  This is very important for the programmer to know because if a lot of memory is allocated in a function, they need to be aware that it will not be garbage collected at the end of the function.  delete will deallocate this memory, so the unnecessary memory does not stick around (although it would be better to just declare global_variable with a var). The second example shows the variable declared as this.global_var.  The this is this case connects the function to the global scope, so this will behave in the same way. 

 

This memory allocation is much different than in C because there is no way to allocate memory to the stack or the heap.  The program does this automatically rather than putting it in the hands of the programmer.  The user therefore has less ability to deallocate memory from the heap and stack to reduce memory leaks and using all of the memory. 

Task 2:

The second part of this project was to try to determine when garbage collection happens by calling a function many times and determining how long it takes to call the function each time. In theory, it should be approximately the same every time.  

It took :  0.435  seconds the  0  iteration

It took :  1.64  seconds the  1  iteration

It took :  0.946  seconds the  2  iteration

It took :  0.675  seconds the  3  iteration

It took :  0.799  seconds the  4  iteration

It took :  0.697  seconds the  5  iteration

It took :  0.717  seconds the  6  iteration

It took :  0.881  seconds the  7  iteration

It took :  0.848  seconds the  8  iteration

It took :  0.715  seconds the  9  iteration

It took :  0.79  seconds the  10  iteration

It took :  0.779  seconds the  11  iteration

It took :  0.791  seconds the  12  iteration

It took :  0.78  seconds the  13  iteration

It took :  0.75  seconds the  14  iteration

It took :  0.765  seconds the  15  iteration

It took :  0.798  seconds the  16  iteration

It took :  0.831  seconds the  17  iteration

It took :  0.764  seconds the  18  iteration

It took :  0.8  seconds the  19  iteration

It took :  0.754  seconds the  20  iteration

It took :  0.792  seconds the  21  iteration

It took :  0.738  seconds the  22  iteration

It took :  0.775  seconds the  23  iteration

It took :  0.736  seconds the  24  iteration

It took :  0.788  seconds the  25  iteration

It took :  0.843  seconds the  26  iteration

It took :  0.802  seconds the  27  iteration

It took :  0.768  seconds the  28  iteration

It took :  0.804  seconds the  29  iteration

It took :  0.804  seconds the  30  iteration

It took :  0.817  seconds the  31  iteration

It took :  0.85  seconds the  32  iteration

It took :  0.767  seconds the  33  iteration

It took :  0.761  seconds the  34  iteration

It took :  0.743  seconds the  35  iteration

It took :  0.745  seconds the  36  iteration

It took :  0.749  seconds the  37  iteration

It took :  0.747  seconds the  38  iteration

It took :  0.759  seconds the  39  iteration

It took :  0.742  seconds the  40  iteration

It took :  0.794  seconds the  41  iteration

It took :  0.737  seconds the  42  iteration

It took :  0.783  seconds the  43  iteration

It took :  0.778  seconds the  44  iteration

It took :  0.786  seconds the  45  iteration

It took :  0.772  seconds the  46  iteration

It took :  0.785  seconds the  47  iteration

It took :  0.806  seconds the  48  iteration

It took :  0.782  seconds the  49  iteration

It took :  0.785  seconds the  50  iteration

It took :  0.787  seconds the  51  iteration

It took :  0.784  seconds the  52  iteration

It took :  0.803  seconds the  53  iteration

It took :  0.784  seconds the  54  iteration

It took :  0.817  seconds the  55  iteration

It took :  0.945  seconds the  56  iteration

It took :  0.789  seconds the  57  iteration

It took :  0.807  seconds the  58  iteration

It took :  0.759  seconds the  59  iteration

It took :  0.851  seconds the  60  iteration

It took :  0.827  seconds the  61  iteration

It took :  0.939  seconds the  62  iteration

It took :  0.765  seconds the  63  iteration

It took :  0.814  seconds the  64  iteration

It took :  0.757  seconds the  65  iteration

It took :  0.864  seconds the  66  iteration

It took :  0.774  seconds the  67  iteration

It took :  0.805  seconds the  68  iteration

It took :  0.785  seconds the  69  iteration

It took :  0.814  seconds the  70  iteration

It took :  0.957  seconds the  71  iteration

It took :  0.865  seconds the  72  iteration

It took :  0.816  seconds the  73  iteration

It took :  0.822  seconds the  74  iteration

It took :  0.805  seconds the  75  iteration

It took :  0.803  seconds the  76  iteration

It took :  0.79  seconds the  77  iteration

It took :  0.789  seconds the  78  iteration

It took :  0.805  seconds the  79  iteration

It took :  0.799  seconds the  80  iteration

It took :  0.859  seconds the  81  iteration

It took :  0.785  seconds the  82  iteration

It took :  0.802  seconds the  83  iteration

It took :  0.812  seconds the  84  iteration

It took :  0.801  seconds the  85  iteration

It took :  0.828  seconds the  86  iteration

It took :  0.804  seconds the  87  iteration

It took :  0.807  seconds the  88  iteration

It took :  0.803  seconds the  89  iteration

It took :  0.8  seconds the  90  iteration

It took :  0.796  seconds the  91  iteration

It took :  0.804  seconds the  92  iteration

It took :  0.818  seconds the  93  iteration

It took :  0.885  seconds the  94  iteration

It took :  0.844  seconds the  95  iteration

It took :  0.819  seconds the  96  iteration

It took :  0.811  seconds the  97  iteration

It took :  0.83  seconds the  98  iteration

It took :  0.805  seconds the  99  iteration

 

Except the second iteration (which is always significantly longer for some reason), they all take about 0.8 seconds.  There is not one that stands out to say that's when garbage collection happened.  This is because reference count garbage collection is always happening in parallel rather than the program executing stopping and doing all garbage collection that it can in one step (like in Ruby).  I would guess that if the reference count garbage collection was having trouble and almost all the memory was used, then it would stop and switch to using mark and sweep instead.  

 

Extensions

  1. I wrote a haiku to show functions declared in a function without a var key word become global variables so they are not garbage collected. 

  2. I rewrote an example big_obj, small_obj example above in C, imitating how the memory is dealt with in JavaScript:

It starts by making the big_obj as a struct, which is as close to a JavaScript object that C has.  Then I make new_obj point to what big_obj did, freeing big_obj and setting it to null.  Then I create a sec_obj, having it point to new_obj's inner object.  Then I free new_obj and set it to null.  Then I free sec_obj when it gets set to null.  This shows how the memory allocation and deallocation happens in this JavaScript example using C explicit memory allocation and deallocation. It outputs:

 

 

Labels
  • No labels