InsOp
InsOp

Reputation: 2699

Javascript Garbage Collection notification

Is there a way to get notified when the browser triggers garbage collection? (Or is finished with collecting garbage)

In the process of developing a webapp, it would be quite handy to get notified when the used heap thresholded a certain value, or proportion, after garbage collection.

Upvotes: 2

Views: 521

Answers (3)

Redu
Redu

Reputation: 26191

2022

It's a late one but somebody had to give this answer.

It all depends what you want to do by detecting the garbage collection though. Perhaps the closest you can get is to eploy the FinalizationRegistry. Nowadays a reasonable use case is to trash the WASM side refrencing objects once their JS side counterparts get GCed. Cool.

You just create a register with a callback to repond once a registered object gets GCed.

Lets try;

var cb = id => console.log(`The object with ID: ${id} has just been garbage colected`),
    fr = new FinalizationRegistry(cb),
    ab = new ArrayBuffer(1e5),
    cd, ef, gh;

fr.register(ab, "Reg. Id: 001_ab");

ab = null;
    
setTimeout( _ => ( cd = new ArrayBuffer(1e6)
                 , console.log("Forced GC a little bit with cd..!")
                 )
          , 500
          );
    
setTimeout( _ => ( ef = new ArrayBuffer(1e7)
                 , console.log("Forced GC a little bit more with ef..!")
                 )
          , 1000
          );
    
setTimeout( _ => ( gh = new ArrayBuffer(1e8)
                 , console.log("Forced GC a little bit even more with gh..! But wait a little..")
                 , cd = null
                 , ef = null
                 , gh = null
                 )
          , 1500
          );

Upvotes: 1

Muhammad Imran
Muhammad Imran

Reputation: 744

Simple answer: It depends on the browser's implementation (with current browsers: NO)

First, memory management isn't part of the JavaScript, it's part of the environment (e.g, browser). Since JavaScript runs on VM created by the browser so like other languages it's platform's task not the language itself. Like Java, JVM decides when to run GC, not Java.

Second, about the notification part, It depends on the browser's implementation. Currently, no famous browser provides such functionality to notify user when GC has triggered and when it's finished. However, memory information can be acquired manually, during debugging, using browser's profiler.

However, Info about its memory can be find, during program execution, in window object. You can implement your callback function to acquire knowledge about memory usage using window.performance.memory object whenever you wanted.

In chrome it' provide updated info if you run chrome with --enable-memory-info switch.

For Chrome More info can be found here: http://blog.chromium.org/2011/05/chrome-developer-tools-put-javascript.html

Upvotes: 2

the8472
the8472

Reputation: 43115

For unprivileged javascript there is no such API. But while you're developing your software you can use the profiler/debugger tools of various browsers to observe the behavior - including garbage collection - of your code.

Upvotes: 0

Related Questions