What is the best way to free up memory from a TypedArray in JavaScript?

javascript

Solution

Nulling the references to the typed array should be good enough. Setting `.length = 0` does not work, as - in contrast to usual arrays - the `length` property is readonly.

Should you really experience problems with the garbage collector, I would recommend to try reusing the same buffer over and over, instead of allocating new ones all the time and hoping for them to get freed.

Problem

I understand fully that the garbage collector will eventually do it's job and free up the memory allocated in a `TypedArray` in exactly the same way as it would any variable or object (assuming there are no circular references etc). However, I am doing a lot of continuous processing in a number of `WebWorkers` and I would therefore like to have this memory freed as soon as possible by the GC. Using normal `JavaScript` arrays, simply setting `array.length = 0;` is a good way a doing exactly this, but what about when using TypedArray's? Would the following result in the memory being freed as soon as possible? ``` var testArray = new Uint8Array(buffer); ///Do stuff with testArray tesArray.length = 0; ``` Or since the TypedArray is simply a view over the `ArrayBuffer`, would I need to clear the actual buffer itself also? If so, how?

Original source