face-api.js
face-api.js copied to clipboard
detectAllFaces() Leaks Memory in WebWorker
I can confirm that detectAllFaces is leaking memory. I pass frames to a WebWorker with postMessage and transfer lists, then create an ImageData in the WebWorker as shown below.
If I run the code as shown, the Chrome process memory balloons and grows DURING the frame processing. (E.g. while the webworker is inside detectAllFaces. Within ~3 frames, Chrome is using 25% of the RAM and growing. After ~30 seconds, Chrome has consumed nearly all of my 16 GB of RAM.
As soon as I comment out the line that has detectAllFaces - memory usage flatlines. Does not grow or change. And that is the ONLY code change I make - with detectAllFaces, memory leaks. Without that call, no memory leak. Therefore, something is going wrong with detectAllFaces - because there are no other references to the given imgData other than detectAllFaces.
Any ideas on how to diagnose further or, hopefully, fix?
// Earlier in the code ...
faceapi.nets.ssdMobilenetv1.loadFromUri('/models')
this.faceDetectorOptions = new faceapi.SsdMobilenetv1Options({ minConfidence: 0.5 });
// For every message from the parent thread, we do...
const imgData = new ImageData(
new Uint8ClampedArray(message.data),
message.width,
message.height
);
const img = faceapi.createCanvasFromMedia(imgData);
const results = await faceapi.detectAllFaces(img, this.faceDetectorOptions)
Originally posted by @josiahbryan in https://github.com/justadudewhohacks/face-api.js/issues/345#issuecomment-733872505
I think this might be specific to using the cpu tensorflow backend...why?
I'm running my app over VNC on a laptop in latest chrome - in the devtools of that chrome, I see warnings about webgl not supported. Tried using wasm - that failed completely. So logic is that it falls back to the cpu backend.
Running the same code - EXACT same code - in a browser locally on my Mac (same Chrome version, just local - not over VNC) - and no webgl warnings, and I can even use wasm - and no memory leaks. (Using Chrome's Task Manager, the RAM usage for that tab + worker stays flat)
So, something about the CPU backend...? What's up with that...?
Can confirm the memory leak. @josiahbryan could you solve that in the meanwhile?
Never tried to solve CPU memory leak - my use case did not depend on CPU backend because I could reply on using webgl in production.