Hi Apple Engineers,
I am encountering an issue where the memory usage reported by the Xcode memory report and the Xcode Instruments memory profiler are not aligned. Specifically:
Xcode Memory Report:
After implementing autoreleasepool, URLSession reading a zip file, and moving the task inside DispatchQueue.global().async, the memory usage goes down from 900MB to 450MB, indicating a potential memory leak.
Xcode Instruments Memory Profiler:
The memory usage goes down from 900MB to 100MB, suggesting that the memory has been properly released and there is no significant memory leak.
Could you please help me understand the discrepancy between these two tools and provide guidance on the appropriate way to interpret the memory usage in my application? Which result I should rely on it?
I would greatly appreciate your insights and expertise on this matter. Thank you in advance for your assistance.
Post
Replies
Boosts
Views
Activity
In one of our SwiftUI projects, we intensively use UIViewController to present a SwiftUI View modally, and it works perfectly under normal circumstances.
However, we have observed that when screen mirroring is enabled on the iPhone, the @Environment viewControllerHolder becomes nil, preventing the proper presentation of another view. Xcode (14.5) does not flag any issues with the code, and our project is set to build for iOS 17.5.
Without changing too many codebase, is there a way to fix this unexpected issue?
import SwiftUI
import UIKit
struct ContentView: View {
@Environment(\.viewController) private var viewControllerHolder: UIViewController?
@State var presentSecondPage = false
var body: some View {
VStack(spacing: 40) {
Text("This is First Page")
Button("Present Second Page") {
presentSecondPage = true
}
}
.onChange(of: presentSecondPage) {
if presentSecondPage {
viewControllerHolder?.present(style: .fullScreen) {
SecondPage(presentSecondPage: $presentSecondPage)
}
}
}
}
}
struct SecondPage: View {
@Environment(\.viewController) private var viewControllerHolder: UIViewController?
@Binding var presentSecondPage: Bool
var body: some View {
VStack(spacing: 40) {
Text("This is Second Page")
Button("Back to First Page") {
presentSecondPage = false
viewControllerHolder?.dismiss(animated: true)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.gray)
}
}
struct ViewControllerHolder {
weak var value: UIViewController?
}
struct ViewControllerKey: EnvironmentKey {
static var defaultValue: ViewControllerHolder {
let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
let rootVC = windowScene?.windows.first(where: { $0.isKeyWindow })?.rootViewController
return ViewControllerHolder(value: rootVC)
}
}
extension EnvironmentValues {
var viewController: UIViewController? {
get { return self[ViewControllerKey.self].value }
set { self[ViewControllerKey.self].value = newValue }
}
}
extension UIViewController {
func present<Content: View>(style: UIModalPresentationStyle = .automatic, @ViewBuilder builder: () -> Content) {
let toPresent = UIHostingController(rootView: AnyView(EmptyView()))
toPresent.modalPresentationStyle = style
toPresent.rootView = AnyView(
builder()
.environment(\.viewController, toPresent)
)
NotificationCenter.default.addObserver(forName: Notification.Name(rawValue: "dismissModal"), object: nil, queue: nil) { [weak toPresent] _ in
toPresent?.dismiss(animated: true, completion: nil)
}
self.present(toPresent, animated: true, completion: nil)
}
}
Normal Circumstances
Screen Mirroring
Previously I reached out to Apple Support, regarding the purchase of additional code-level support quotas under my Apple Developer Program. Unfortunately, they were unable to provide an answer and directed me to contact the Code-Level Support team for further assistance. And Code-Level Support advised me to post questions on the Developer Forum.
Here I have two inquiries regarding Code-Level Support:
I understand that each Apple Developer Program subscription includes 2 Code-Level Support instances per year. How can we check our remaining support balance online?
If I exhaust my support quota, what is the procedure for purchasing additional support?
Thank you for any assistance.