1

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!

aman
  • 47
  • 5
  • So, I did one more thing, and seems like it is working, not a conventional way though, I would really this to be handle by chrome's garbage collector(GC) but here it is what I did - I passed the buffer back to main thread as a transferrable object and explicitly set it to null. It worked for now, not shooting up memory as well. But I would really appreciate if someone can shed more light on this, about this memory handling concept of the browser, what am I missing here, that would be great. And, any other possible solution for this. – aman Jun 03 '22 at 06:11
  • Does your tab crash eventually? If so, open a bug report. – Kaiido Jun 05 '22 at 22:59
  • @Kaiido yeah it does sometimes on lower end phones on chrome('Aw, snap!'), but safari on iPhone is giving memory exceeded error, looks like GC is not kicking or there is a memory leak – aman Jun 07 '22 at 07:26

1 Answers1

2

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.

aman
  • 47
  • 5
  • 1
    I have found their garbage collector to also do this with transferable objects. My scenario had high cpu load, so I assume that maybe the GC wasn't being allowed to start, to conserve cpu, but I do not know the heuristics of its algorithm. https://bugzilla.mozilla.org/show_bug.cgi?id=1407691#c25 – Joel Teply Feb 25 '23 at 23:25