Arkit + Reality Kit memory de-allacation

Hello, i have an AR app and i want to change from rear-facing camera (ARWorldTrackingConfiguration()) to front-facing camera (ARFaceTrackingConfiguration()) and the opposite. I am using ARkit+RealityKit. My problem here is that every time my UI refreshes (i press a button to change the configuration as a result to change rear or front facing camera), the memory never de-allocating, so with 5-6 times back and forth, i get 1,2 GB RAM without doing nothing at all and my app crashes.

Also, there are no memory leaks.

Any clue why there isnt memory de-allocation in ARkit or RealityKit?

Replies

Hello,

It seems that you are encountering an unbounded memory growth issue. I was not able to reproduce it as you described (toggling an ARView's underlying ARSession between an ARWorldTrackingConfiguration and an ARFaceTrackingConfiguration), so I recommend that you double check your code related to changing between these configurations, as you may have a retain cycle keeping something alive unexpectedly, or you may have abandoned a strong reference to an object (or many objects) that is keeping a large amount of IOSurface alive.

Please see this video about detecting and diagnosing memory issues: https://developer.apple.com/videos/play/wwdc2021/10180/

Here is a simple app with memory problem. I call ARViewRepresentable() class and my CustomARView() to change the configuration on the fly with the help of an observable object. But it doesn't de-allocate the memory. AppSample

I'm not seeing unbounded memory growth in your sample project, it appears to oscillate between 300 MB and 120 MB. (tested on an iPhone 13 Pro (iOS 15.2.1)

Is there something unique about your configuration that you think may be needed to reproduce the issue?

Also, I'd like to point out that, instead of recreating your ARView every time you switch configurations, it would be more efficient to keep the same ARView, and just run your new configuration on that ARView's ARSession.