Memory Leak: JavaScript - passing array buffer to Web Worker using transferable object, GC doesn't kick in

616 views Asked by At

So, I am passing video buffer(Float32Array buffer) to worker thread as a transferable object, it happens for each frame of the video(meaning, continuously video frames are being passed to worker). I did memory profiling, and worker thread is retaining the object, it seems garbage collector doesn't kick in. How to handle this case? I don't think one can invoke GC explicitly in JavaScript.

Here is the code snippet how I am passing video buffer to worker thread:

this.worker.postMessage({
          command: 'SetVideoBuffer',
          data: {
            videoFrame: videoFrame
          }
        },
          [videoFrame.buffer]);

Also, I tried setting the buffer at worker side to 'null', it didn't help.

Any idea how to resolve this, is this chrome thing? Please help.

Thank you!

1

There are 1 answers

1
aman On BEST ANSWER

So, I got the workaround of this problem, looks like if one sends back the video buffer back to main thread where it was created, GC does kick in and clear the heap.

By using the same, zero-copy(transferable objects) way, I transferred the buffer back to the main thread, memory leak issue got resolved.