SwiftUI: memory spike when keyboard is presented

I am working with a UIKit app, which has an MKMapView in it, which displays a bunch of MKAnnotationViews. For each annotation view, I'm using a SwiftUI view to draw the detailAccessoryView. This works, but whenever a keyboard is presented (even if it's in another view controller), I see a major memory spike in the app. For e.g., it goes from using 130MB to 600MB+ on an iPhone8 (the exact values don't matter of course). This explains a crash that seems to be happening with the same app in production as well, which I reported in this DevForums Thread

From what I can tell, whenever a keyboard is shown, the SwiftUI views seem go repeatedly resize or re-layout themselves, maybe trying to reconfigure their internal layout or something. If I reduce the number of views in my SwiftUI view, it reduces the memory spike; if I remove the SwiftUI view and just use a plain UIView, the spike completely disappears.

My question is: how can I get the SwiftUI views to ignore what's happening with the keyboard in some other view? I've read in some other threads that there is a 'keyboard avoidance' issue with iOS14 and above ... though in my SwiftUI view, there isn't any text field in it's own views. I've tried adding the modifier to the top-level SwiftUI view:

.ignoresSafeArea(.keyboard)

But it doesn't make a difference

Can anyone shed some light on this? Is there another way to avoid keyboard changes in a SwiftUI view?

I'm seeing the exact same thing. Except my memory spikes up around 1.8 gb. I tried the .ignoresSafeArea, but it made no difference.

I'm able to reproduce this on iOS 14 and 15 as well (although not by any means reduced to its simplest form). I also see memory spike temporarily to 1.5 to 2GB which almost always causes the app to be terminated. All I can conclude so far is that presentation of the keyboard is an important ingredient.

It'd be great to hear of a workaround if anyone has found one.

I found that if I disabled animations I could prevent the memory spike. This leads to a less visually appealing experience - but at least it doesn't crash.

SwiftUI: memory spike when keyboard is presented
 
 
Q