Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to free up the memory in JavaScript

I'm working with canvas and its ImageData object which contains a huge amount of data (millions of integers). So working with a few arrays already takes a lot of memory (up to 300MB). Is there a way to free up the memory of some array when it's unnecessary? I'm trying to assign undefined to that variable. Is it right?

like image 526
haynar Avatar asked Dec 11 '11 20:12

haynar


People also ask

Do you have to free memory in JavaScript?

In contrast, JavaScript automatically allocates memory when objects are created and frees it when they are not used anymore (garbage collection). This automaticity is a potential source of confusion: it can give developers the false impression that they don't need to worry about memory management.

Where is memory stored in JavaScript?

JavaScript engines have two places where they can store data: The memory heap and stack. Heaps and stacks are two data structures that the engine uses for different purposes.

What are memory leaks in JavaScript?

What are memory leaks? In simple words, a memory leak is an allocated piece of memory that the JavaScript engine is unable to reclaim. The JavaScript engine allocates memory when you create objects and variables in your application, and it is smart enough to clear out the memory when you no longer need the objects.

How do I clear memory in node JS?

So how would I do that? There is no memory management in js, you can make sure that the object you created are not being used anywhere in the code, then the garbadge collector will free them. It happens automatically. Once your job is finished, the process will be terminated and garbage collector takes care of it.


2 Answers

If the variable persists (e.g. it's global or part of some persistent data structure) and the data it points to is large and you want that data to be eligible for garbage collection, then you are correct to assign something small to that variable. undefined or null or "" will all work. What you're doing is clearing the reference to the large data so that it will be eligible for garbage collection. If nothing else in your javascript has a reference to that data, then it can be freed by the garbage collector. If anything else has a reference to it, then it cannot be freed.

For example, if you had a 10,000 element array held in a global variable:

var largeDataArray = new Array(10000); 

And, you had filled most elements with data, then you could allow that memory to be eligible for garbage collection by assigning it some other value like:

largeDataArray = null; 

or if you still want it to be an array:

largeDataArray = []; 

Note: variables that themselves go out of scope (like local variables in functions that aren't part of a lasting closure) or variables in objects that themselves go out of scope do not have to be manually cleared. When they go out of scope or when the parent object is deleted, the data contained within will also be eligible for garbage collection.

So, the clearing of a variable only needs to be done when you explicitly want to free data that is held in a long lasting variable and it's usually only relevant to worry about this when the data is large or you have a lot of them that add up to multiple megabytes of data (memory use is of higher concern at lower levels on smartphones than in desktop browsers).

like image 170
jfriend00 Avatar answered Sep 27 '22 21:09

jfriend00


JavaScript has automatic memory management. Memory containing objects which are no longer referenced will be eligible for garbage collection, unless you have a memory leak. There is generally no need to manually assign undefined to variables.

If your program is using too much memory, you should shrink the arrays to get rid of elements you no longer need. See Array.pop, Array.shift, and Array.splice.

like image 39
Matt Ball Avatar answered Sep 27 '22 19:09

Matt Ball