Skip to content Skip to sidebar Skip to footer

Javascript - Garbage Collector Timers?

Any idea on garbage collector timer in javascript? Suppose i run below script, will function and associated scope chained variable will go for garbage collection exactly after 100

Solution 1:

Every user agent implements garbage collection differently. All user agents use the mark-and-sweep method on a periodic repetition, so there is no "instantly" about it; it will happen when it happens.

Each agent has different thresholds and mechanisms to determine when the GC does a pass. It isn't necessarily event-driven (purhaps you might say it is benchmark-driven, event-initiated), and certainly not based on a timer.

A function that passes out of scope is instantly eligible for garbage collection, but there's really no telling when it would happen.

This is really something that, from the developer perspective, you are not intended to think about. There isn't any way to stop or start GC, or any indication that it happened at all. Check out about:memory in Firefox for some interesting trivia (and there's a couple of dubious buttons down there to "control" the GC). That's about all you're going to get as far as it goes under the hood, and that data isn't available to scripts.


Solution 2:

The garbage collector is non-deterministic.
Garbage will be collected some time after it becomes garbage.

A closure object passed to setTimeout will become garbage after it executes.

Anything beyond that is implementation-specific.


Post a Comment for "Javascript - Garbage Collector Timers?"