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?
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?
- 1 Why not use testArray = null? I have seen this approach being used a lot. – FailedUnitTest Commented May 11, 2017 at 14:22
- 1 If nulling the array is indeed the best way to do so, and is actually effective, then great, i'll use that. But my concern was that the actual underlying buffer itself gets left behind and cleaned up by the GC later. – gordyr Commented May 11, 2017 at 14:24
- Good point, I thought that GCs nowaways are smart enough to take care of this. – FailedUnitTest Commented May 11, 2017 at 14:26
- Indeed, you're quite right, that's very likely to be the case. But I was hoping someone had some insight or experience that could offer a more plete explanation of what modern browsers actually do behind the scenes with regards to TypedArray's. – gordyr Commented May 11, 2017 at 14:31
2 Answers
Reset to default 6Nulling 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 remend to try reusing the same buffer over and over, instead of allocating new ones all the time and hoping for them to get freed.
Following @Bergi's advice, and @vitaly-t's note, I came up with this:
/**
* Dereferences a list of objects and prepares them for garbage collection
* @simply PrepareForGarbageCollection(...objects:any) → void
*
* @param {...any} objects The list of objects to dereference
*/
function PrepareForGarbageCollection(...objects) {
// @performance
for(let object of objects) {
if(object === void null || object === null)
continue;
if([Map, WeakMap, Set, WeakSet].find(constructor => object instanceof constructor))
object.clear();
else if([Array, Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array, Int8Array, Int16Array, Int32Array, Float32Array, Float64Array, BigInt64Array, BigUint64Array].find(constructor => object instanceof constructor))
object.fill(0, 0, object.length - 1);
else if(object instanceof Object)
for(let key in object) {
// @deep → PrepareForGarbageCollection(object[key]);
delete object[key];
}
delete object;
}
}
- It dereferences as much crap as possible and empties huge arrays
- It dereferences the object(s) as well