2
votes

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 100ms? Or some margin?

I read one thread about garbage collection in stackoverflow, still i have this question. Below are my questions?

  1. Does any SYSTEM TIMER run for garbage collection task?
  2. If no, is it EVENT based?, means if reference is no more present, garbage collector will reclaim memory INSTANTLY.

    function call_me() {
    //calculate elapsed_time - code not given
    
               if(elapsed_time <100)
               {
                setTimeout(call_me,25);
               }
              else{
               final_call();
              }
    }
    
    call_me();
    
2
Why does it matter? The whole point of garbage collection is that it's implicit and not something you really need to worry about. It's only up to you to make things garbage by dereferencing them from everything else. What happens after that is likely very implementation specific for each JS engine. - Alex Wayne
@Praveen: The question is not relevant; by definition, you don't need to worry about the GC. It is also implementation-specific. - SLaks
not relevant to whom? A programmer or who want to know the internal design. - P K
When you're writing in javascript, as I mentioned in my answer, you have absolutely, 100% no access or control over GC. Indeed, from the perspective of the script, GC does not exist. That is why, in a question about javascript, GC is not relevant. If you were asking a question about programming a user agent, it is quite relevant, but when it comes to javascript, if you can't use it, can't affect it, can't even reference it, then how is it possibly relevant? If your question is about avoiding memory leaks, I'd ask about that instead. - Chris Baker
@RacilHilan I reviewed the mechanics of GC a little in my answer, but your disagreement here seems misplaced and unfounded. The document you link does not demonstrate any type of control over GC. You appear to be conflating memory management as a coding practice with garbage collection as a user agent mechanic. There is, simply, no API to control the garbage collector. - Chris Baker

2 Answers

7
votes

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.

3
votes

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.