🐛 Possible frame processor memory leak on iOS
What were you trying to do?
Been investigating memory issues on my app, created a stripped down repo to try and find the culprit through trial and error.
I observed running an empty frame processor on iOS slowly increases memory without it being released.
This effect is multiplied if the frame processor does work using a frame processor plugin and/or calls over to react.
I created a repo here https://github.com/alexstanbury/RNVC-Leak
What happened instead?
Memory increased over time without being released.

How to reproduce?
yarn && cd ios && pod install && cd ..
Open app, let it settle for a few seconds, observe memory usage, toggle camera and leave it to run for a while, observe memory increase. Toggle camera again, observe memory baseline gradually increases after each toggle on/off.
Relevant log output
No response
Device
iPhone XR
VisionCamera Version
2.13.3
Additional information
- [ ] I am using Expo
- [X] I have read the Troubleshooting Guide
- [X] I agree to follow this project's Code of Conduct
- [X] I searched for similar issues in this repository and found none.
Huh, interesting! I assume this should be reproduceable in the example app here as well?
Do you notice something similar on Android?
I've done a bit more digging and testing. I don't see any issues on Android, memory gets used but then cleared by the GC as expected
With my repo on iOS in release mode, these are the kind of mallocs that are building up:

I tried with the example on this repo (I added a button to show/hide the camera and tested in debug and release mode) and that's where I noticed that the differentiator is having Hermes enabled. In both my repo and this repo example, switching Hermes on causes the memory increase on iOS, after switching it off memory is allocated and released as expected.
My repo (debug mode) with hermes:

My repo (debug mode) without hermes:

This repo (release mode) with hermes:

I'm not really sure where to go from here, I have a feeling it may be related to reanimated as there are some issues over there that look like they may be contributing.
I think I found the minimal reproducing step. Here is the repository.
As you can see in the repo, useFrameProcessor recreates the frame processor on every change of foo value.
I suppose that the object returned by useFrameProcessor is not being cleaned up well, and it causes the leak.
The example is with V3. However, you can reproduce the leak in the same way with V2 also.
Hey thanks for your research @alexstanbury and thanks for your repro @bglgwyng - appreciate it! I'll investigate this as part of the V3 efforts!
I've been trying to write my first react native app using react-native-vision-camera, but my app has been crashing on android without leaving an error message at all (just disappears on the device - no sign in console that it has crashed). I eventually discovered that commenting out the frame processor from the Camera properties in the return value stops it from crashing so I suspect it is the same bug that this issue number refers to.
Unfortunately I'm trying to do face detection (then use react-native-sound to play a sequence of sounds) so I need the frame processor for the face detection, but it almost never makes it to the end of the sound sequence. I suspect that this bug (1098) may be the same bug I'm facing, but I'm facing it on Android.
I tried reanimated 2.17.0 (as well as the reanimated 2.14.4 that I downgraded to initially, to get around this bug: https://github.com/mrousavy/react-native-vision-camera/issues/1514) and the issue still occurs. But as described in 1514, reanimated 3.x isn't supported by react-native-vision-camera (Property '_setGlobalConsole' doesn't exist) so I don't know if 3.x would fix the problem.
In the meantime I have had to resort to using a different camera library for face recognition (which is playing nicely with react-native-sound fortunately, hence I'm pretty sure my issue is with using react-native-vision-camera and specifically it is the frame processor's fault, rather than react-native-sound). Would report this as a reanimated bug, but given they're on 3.x now it may have been fixed.
So I'm posting this comment just so that if there are others who follow the same path I did, they don't get stuck thinking they've done something wrong (I spent hours trying to figure out whether I hadn't release()d a Sound object in some edge case). Unfortunately the current version of react-native-vision-camera relies on react-native-reanimated 2.x, and react-native-reanimated 2.x has memory leak bugs that affect android as well as iOS.
Looking forward to V3 (without reanimated), and thanks for all your efforts!
Closing as this is a stale issue - this might have been fixed with the full rewrite in VisionCamera V3 (🥳) - if not, please let me know here!!