#if DEBUG import Foundation import Darwin /// Fixture instrumentation contains only process memory numbers, never inputs. @MainActor public final class ShareMemory { private var peak: UInt64 = 0 private var sampling: Task? public init() { sampling = Task { [weak self] in while !Task.isCancelled { self?.sample() try? await Task.sleep(for: .milliseconds(10)) } } } private func sample() { var info = task_vm_info_data_t() var count = mach_msg_type_number_t(MemoryLayout.size / MemoryLayout.size) let status = withUnsafeMutablePointer(to: &info) { pointer in pointer.withMemoryRebound(to: integer_t.self, capacity: Int(count)) { task_info(mach_task_self_, task_flavor_t(TASK_VM_INFO), $0, &count) } } if status == KERN_SUCCESS { peak = max(peak, info.phys_footprint) } } public func save(to url: URL) { sample(); sampling?.cancel() var usage = rusage(); getrusage(RUSAGE_SELF, &usage) let data = try? JSONSerialization.data(withJSONObject: ["peak_physical_footprint_bytes": peak, "peak_rss_bytes": UInt64(usage.ru_maxrss)], options: .sortedKeys) try? data?.write(to: url, options: .atomic) } deinit { sampling?.cancel() } } #endif