memory management - How is garbage collection triggered in JavaScript? -
consider javascript code below. creating array of let's 4 elements , delete reference it. when garbage collection happen? know specific language implementation, have not many javascript engines.
edit: simplest possible case, interests me because garbage collection causes audible glitches in web audio applications.
var = [1, 2, 3, 4]; = null; // other code update: javascript , garbage collection not explain sequence of events , how triggered. don't want control garbage collection. need better understanding design better code.
there many ways gcs can triggered
- allocation-triggering: there no more room in allocation buffers/young generation/howeveritiscalled , gc required free objects
this gets bit more complicated in mixed gc/manual allocation environment. malloc wrappers might need trigger gcs since javascript can hold onto manually allocated resources. - [in browsers] when tab/window gets closed/replaced on assumption there lots of easy-to-collect garbage @ point
- time-triggered heuristics incremental collections meet pause time goals
- privileged javascript may able trigger collections directly
- as last-ditch effort various non-gc-managed components if run out of native resources (file handles, virtual address space) in hope gc objects awaiting finalization holding onto them
there other reasons can think of. it's not should concern with.
if want keep gcs low need keep object allocation rate low. e.g. algorithms can benefit pre-allocated buffers on perform work. effectiveness of strategy depends on whether javascript runtime has escape analysis , how effective @ avoiding short-lived allocations in first place. , on whether collector generational. generational collector suffers lot less rapid, short-lived allocations.
Comments
Post a Comment