8

I cannot manage to release my RealityKit ARView() from memory. I am aware that there were similar issues with ARKit + SceneKit with workarounds which doesn't solve my problem, unfortunately.

The solutions above kind of work by removing everything "suspicious" manually. That is exactly what I did in an even wider scope:

func closeCurrentView(completion: (() -> Void)? = nil, isScanAnotherFloor: Bool = false) {
    if backgroundRenderingID != UIBackgroundTaskIdentifier.invalid {
        let app = UIApplication.shared
        app.endBackgroundTask(self.backgroundRenderingID)
        self.backgroundRenderingID = UIBackgroundTaskIdentifier.invalid
    }
    self.arView?.session.pause()
    self.arView?.session.delegate = nil
    self.arView?.scene.anchors.removeAll()
    self.arView?.removeFromSuperview()
    self.arView?.window?.resignKey()
    self.arView = nil
}

Memory will rise to 90MB to 250MB and once deinit is called it will reduce to 175MB, not clearing all the memory.

Also at the time of initializing, I set proper options too.

arView?.renderOptions = [
    .disableMotionBlur,
    .disableDepthOfField,
    .disablePersonOcclusion,
    .disableGroundingShadows,
    .disableFaceOcclusions,
    .disableHDR
]

But still no luck.

enter image description here

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Chirag Shah
  • 3,034
  • 1
  • 30
  • 61

1 Answers1

5

About memory leakage

Alas, RealityKit 2.0 / 1.0 developers can't deallocate ARView from heap memory because there is a poor-quality implementation on Apple's part. Even if you declare arView property as weak in SwiftUI, this instance will be immediately deallocated, so you can't use it. In UIKit, however, declaring arView as weak has no effect.

As you remember, ARC was firstly introduced in Objective-C and later implemented in Swift. It's quite possible that the weak and unowned functionality is not implemented for ARView for some reason, perhaps because RealityKit is tailored for Swift only – @objc attribute explicitly states this. It looks like ARView weak reference isn't tracked by ARC (or there's no an associated Side Table).

@available(macOS 10.15, iOS 13.0, *)
@objc open class ARView : ARViewBase { ... }

If you need more info, please read this post for details.

(Tested in Xcode 14.3)

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • 1
    thanks for a quick answer does anyone find any workaround for this issue? – Chirag Shah Apr 13 '22 at 12:25
  • 1
    I haven't seen any workaround for this problem yet... – Andy Jazz Apr 13 '22 at 12:27
  • 2
    I don't think there is supposed to be a workaround. This is because the memory from an old ARView has similar properties as a new ARView you might instantiate in the future. I guess it's just to make the ARView load faster when doing repeated AR tasks with different or reusing ARViews. I have noticed that if I repeatedly instantiate one ARView after ending the previous one, the memory usage does not increase and stays relatively the same. – Bhavin p Jan 11 '23 at 21:00