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?
To release memory, assign the global variable to null . window. users = null; I want to make this article as easy to understand as possible.
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.
JavaScript typed arrays are array-like objects that provide a mechanism for reading and writing raw binary data in memory buffers. Array objects grow and shrink dynamically and can have any JavaScript value. JavaScript engines perform optimizations so that these arrays are fast.
In contrast, JavaScript automatically allocates memory when objects are created and frees it when they are not used anymore (garbage collection).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With