Pause view updates while no window is visible; fix zero-value flicker
The window left open all day grew the app by ~360 MB in 24 h even after the .tabItem fix. Isolating each piece in lab builds showed the growth is the sum of several small framework leaks on macOS 26.6 that fire on every view update: SwiftUI Table cell updates, AppKit text-cell KVO dependencies (NSKeyValueDependency) and an ObservationRegistrar created inside DesignLibrary. With the window closed every counter stayed flat; minimized or fully covered, SwiftUI kept updating and kept leaking. - WindowVisibility tracks whether any TopManager window is on screen (occlusion state; the status item's host window is ignored). - SystemMonitor records into an always-current LiveState and only mirrors it into the @Published properties while something is visible. Resuming publishes everything at once, so views catch up immediately. Metrics history, alerts and the menu bar keep running while paused. - MenuBarStatus carries the pre-formatted menu-bar strings, republished only when the visible text changes. - AlertCenter assigns healthScore/diagnosis only when they change. - Monitoring now starts in applicationDidFinishLaunching instead of the main window's onAppear, so it also runs when the app launches with no window (state restoration after the window was closed). Also fixes a visible bug: ProcessMonitor's lightweight refresh (2 of every 3 fetches) emitted 0 for memory, CPU and threads instead of the last known values, so ~90% of rows showed "Zero KB" most of the time. Verified in an isolated lab instance: minimized or covered, all leak counters stay flat while the menu-bar title keeps updating; a partially visible window updates every tick. Growth while the window is actually visible (~190 KB/min) is unchanged — those leaks are in Apple's frameworks. 58/58 tests pass, including a regression test that fails on the old code (472 of 487 own processes reported 0 memory). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
35
TopManagerTests/MenuBarStatusTests.swift
Normal file
35
TopManagerTests/MenuBarStatusTests.swift
Normal file
@@ -0,0 +1,35 @@
|
||||
import XCTest
|
||||
@testable import TopManager
|
||||
|
||||
final class MenuBarStatusTests: XCTestCase {
|
||||
|
||||
private func cpu(_ usage: Double) -> CPUInfo {
|
||||
CPUInfo(globalUsage: usage, userUsage: usage, systemUsage: 0, idleUsage: 100 - usage, coreUsages: [])
|
||||
}
|
||||
|
||||
@MainActor
|
||||
func testFormatsVisibleText() {
|
||||
let status = MenuBarStatus()
|
||||
status.update(cpu: cpu(41.6), memory: nil, network: nil)
|
||||
XCTAssertEqual(status.cpuText, "42%")
|
||||
XCTAssertNil(status.memoryText)
|
||||
XCTAssertNil(status.downloadText)
|
||||
}
|
||||
|
||||
/// Every publish re-measures the status item, so a refresh that renders the
|
||||
/// same text must not publish at all.
|
||||
@MainActor
|
||||
func testRepublishesOnlyWhenVisibleTextChanges() {
|
||||
let status = MenuBarStatus()
|
||||
var publishes = 0
|
||||
let subscription = status.objectWillChange.sink { publishes += 1 }
|
||||
defer { subscription.cancel() }
|
||||
|
||||
status.update(cpu: cpu(41.6), memory: nil, network: nil)
|
||||
XCTAssertEqual(publishes, 1)
|
||||
status.update(cpu: cpu(42.4), memory: nil, network: nil) // still "42%"
|
||||
XCTAssertEqual(publishes, 1)
|
||||
status.update(cpu: cpu(57), memory: nil, network: nil)
|
||||
XCTAssertEqual(publishes, 2)
|
||||
}
|
||||
}
|
||||
@@ -92,4 +92,21 @@ final class ProcessDataTests: XCTestCase {
|
||||
XCTAssertNotEqual(make(energy: 5), make(energy: 9))
|
||||
XCTAssertEqual(make(energy: 5), make(energy: 5))
|
||||
}
|
||||
|
||||
// MARK: - Live sampling
|
||||
|
||||
/// Fetches 1–3 are full refreshes; 4 and 5 are lightweight ones, which used
|
||||
/// to report 0 memory for every idle process (~90% of rows). Only processes
|
||||
/// spawned between fetches may legitimately have no reading yet.
|
||||
func testLightweightRefreshKeepsLastKnownMemory() {
|
||||
let monitor = ProcessMonitor()
|
||||
let me = NSUserName()
|
||||
for fetch in 1...6 {
|
||||
let own = monitor.fetchProcesses().filter { $0.user == me }
|
||||
XCTAssertFalse(own.isEmpty)
|
||||
let zero = own.filter { $0.memoryUsage == 0 }.count
|
||||
XCTAssertLessThan(zero, max(3, own.count / 10),
|
||||
"fetch \(fetch): \(zero) of \(own.count) own processes reported 0 memory")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user