Edit: it is the simplest possible case, but it interests me because garbage collection causes audible glitches in some web audio applications.
var a = [1, 2, 3, 4];
a = null;
// other code
Best How To :
There are many ways GCs can be triggered
- allocation-triggering: there is no more room in the allocation buffers/young generation/howeveritiscalled and a GC is required to free up objects
- [in browsers] when a tab/window gets closed/replaced on the assumption that there is lots of easy-to-collect garbage at that point
- time-triggered by heuristics for incremental collections to meet pause time goals
- as last-ditch effort by various non-GC-managed components if they run out of native resources (file handles, virtual address space) in the hope that GC objects awaiting finalization were holding onto them
There probably are other reasons that I currently can think of. It's generally not something you should concern yourself with.