Skip to content

Free, open-source macOS cleaner. CleanMyMac alternative with zero telemetry. Native SwiftUI, scheduled auto-cleaning, Xcode/Homebrew/system cache cleanup. MIT licensed.

fix(performance): keep known members' CPU when an app gains a process

momenbaselb6e2345c1211
diff --git a/PureMac/Services/ProcessResourceMonitor.swift b/PureMac/Services/ProcessResourceMonitor.swift
index 08aa030..bfc157d 100644
--- a/PureMac/Services/ProcessResourceMonitor.swift
+++ b/PureMac/Services/ProcessResourceMonitor.swift
@@ -9,6 +9,9 @@ struct ProcessResourceConsumer: Identifiable, Sendable {
var cpuFraction: Double?
var residentBytes: UInt64
var processCount: Int
+ /// A member without an interval yet (new process or recycled PID) leaves
+ /// the group's CPU as the sum of its measured members only.
+ var cpuIsPartial = false
}
enum ProcessResourceSort: String, CaseIterable, Identifiable {
@@ -65,18 +68,18 @@ enum ProcessResourceMath {
group.residentBytes = group.residentBytes.addingReportingOverflow(reading.residentBytes).overflow
? UInt64.max : group.residentBytes + reading.residentBytes
group.processCount += 1
- // Any unknown member means this group's first interval is incomplete.
- if let total = group.cpuFraction, let cpu {
- group.cpuFraction = min(1, total + cpu)
+ if let cpu {
+ group.cpuFraction = min(1, (group.cpuFraction ?? 0) + cpu)
} else {
- group.cpuFraction = nil
+ group.cpuIsPartial = true
}
groups[id] = group
} else {
let name = application.map { URL(fileURLWithPath: $0).deletingPathExtension().lastPathComponent }
?? reading.name
groups[id] = ProcessResourceConsumer(id: id, name: name, applicationPath: application,
- cpuFraction: cpu, residentBytes: reading.residentBytes, processCount: 1)
+ cpuFraction: cpu, residentBytes: reading.residentBytes, processCount: 1,
+ cpuIsPartial: cpu == nil)
}
}
return Array(groups.values)
diff --git a/PureMac/Views/Tools/ProcessResourcesView.swift b/PureMac/Views/Tools/ProcessResourcesView.swift
index 5bf47b4..acbda7e 100644
--- a/PureMac/Views/Tools/ProcessResourcesView.swift
+++ b/PureMac/Views/Tools/ProcessResourcesView.swift
@@ -178,8 +178,10 @@ private struct ProcessResourceRow: View {
.help(consumer.applicationPath ?? consumer.name)
Spacer(minLength: 8)
VStack(alignment: .trailing, spacing: 5) {
- Text(consumer.cpuFraction.map { $0.formatted(.percent.precision(.fractionLength(1))) } ?? "—")
+ Text(cpuText)
.monospacedDigit()
+ .help(consumer.cpuIsPartial && consumer.cpuFraction != nil
+ ? String(localized: "Some of this app's processes have no CPU reading yet, so this total is partial.") : "")
ProgressView(value: consumer.cpuFraction ?? 0)
.tint(Tint.accent)
.accessibilityHidden(true)
@@ -198,4 +200,11 @@ private struct ProcessResourceRow: View {
.padding(.vertical, 10)
.accessibilityElement(children: .combine)
}
+
+ /// A trailing plus marks a group whose CPU is the sum of its measured members only.
+ private var cpuText: String {
+ guard let cpu = consumer.cpuFraction else { return "—" }
+ let value = cpu.formatted(.percent.precision(.fractionLength(1)))
+ return consumer.cpuIsPartial ? value + "+" : value
+ }
}
diff --git a/PureMac/ar.lproj/Localizable.strings b/PureMac/ar.lproj/Localizable.strings
index 7274c97..341f08d 100644
--- a/PureMac/ar.lproj/Localizable.strings
+++ b/PureMac/ar.lproj/Localizable.strings
@@ -914,6 +914,7 @@
"Settings to review" = "Settings to review";
"Show all apps and processes" = "Show all apps and processes";
"Show top 8" = "Show top 8";
+"Some of this app's processes have no CPU reading yet, so this total is partial." = "Some of this app's processes have no CPU reading yet, so this total is partial.";
"Some processes started or stopped since the last sample. Their values appear in the next one." = "Some processes started or stopped since the last sample. Their values appear in the next one.";
"Sort by" = "Sort by";
"System processes are not listed. Use Activity Monitor for those." = "System processes are not listed. Use Activity Monitor for those.";
diff --git a/PureMac/de.lproj/Localizable.strings b/PureMac/de.lproj/Localizable.strings
index 8148b5a..fabadd2 100644
--- a/PureMac/de.lproj/Localizable.strings
+++ b/PureMac/de.lproj/Localizable.strings
@@ -924,6 +924,7 @@
"Search apps and processes" = "Search apps and processes";
"Show all apps and processes" = "Show all apps and processes";
"Show top 8" = "Show top 8";
+"Some of this app's processes have no CPU reading yet, so this total is partial." = "Some of this app's processes have no CPU reading yet, so this total is partial.";
"Some processes started or stopped since the last sample. Their values appear in the next one." = "Some processes started or stopped since the last sample. Their values appear in the next one.";
"Sort by" = "Sort by";
"System processes are not listed. Use Activity Monitor for those." = "System processes are not listed. Use Activity Monitor for those.";
diff --git a/PureMac/en.lproj/Localizable.strings b/PureMac/en.lproj/Localizable.strings
index aa98b51..bf87ebd 100644
--- a/PureMac/en.lproj/Localizable.strings
+++ b/PureMac/en.lproj/Localizable.strings
@@ -914,6 +914,7 @@
"Settings to review" = "Settings to review";
"Show all apps and processes" = "Show all apps and processes";
"Show top 8" = "Show top 8";
+"Some of this app's processes have no CPU reading yet, so this total is partial." = "Some of this app's processes have no CPU reading yet, so this total is partial.";
"Some processes started or stopped since the last sample. Their values appear in the next one." = "Some processes started or stopped since the last sample. Their values appear in the next one.";
"Sort by" = "Sort by";
"System processes are not listed. Use Activity Monitor for those." = "System processes are not listed. Use Activity Monitor for those.";
diff --git a/PureMac/es.lproj/Localizable.strings b/PureMac/es.lproj/Localizable.strings
index 70f765a..ee0f11e 100644
--- a/PureMac/es.lproj/Localizable.strings
+++ b/PureMac/es.lproj/Localizable.strings
@@ -914,6 +914,7 @@
"Settings to review" = "Settings to review";
"Show all apps and processes" = "Show all apps and processes";
"Show top 8" = "Show top 8";
+"Some of this app's processes have no CPU reading yet, so this total is partial." = "Some of this app's processes have no CPU reading yet, so this total is partial.";
"Some processes started or stopped since the last sample. Their values appear in the next one." = "Some processes started or stopped since the last sample. Their values appear in the next one.";
"Sort by" = "Sort by";
"System processes are not listed. Use Activity Monitor for those." = "System processes are not listed. Use Activity Monitor for those.";
diff --git a/PureMac/fr.lproj/Localizable.strings b/PureMac/fr.lproj/Localizable.strings
index 24ea2c5..d1ef10c 100644
--- a/PureMac/fr.lproj/Localizable.strings
+++ b/PureMac/fr.lproj/Localizable.strings
@@ -910,6 +910,7 @@
"Settings to review" = "Settings to review";
"Show all apps and processes" = "Show all apps and processes";
"Show top 8" = "Show top 8";
+"Some of this app's processes have no CPU reading yet, so this total is partial." = "Some of this app's processes have no CPU reading yet, so this total is partial.";
"Some processes started or stopped since the last sample. Their values appear in the next one." = "Some processes started or stopped since the last sample. Their values appear in the next one.";
"Sort by" = "Sort by";
"System processes are not listed. Use Activity Monitor for those." = "System processes are not listed. Use Activity Monitor for those.";
diff --git a/PureMac/ja.lproj/Localizable.strings b/PureMac/ja.lproj/Localizable.strings
index 1600b6a..03ec4b0 100644
--- a/PureMac/ja.lproj/Localizable.strings
+++ b/PureMac/ja.lproj/Localizable.strings
@@ -914,6 +914,7 @@
"Settings to review" = "Settings to review";
"Show all apps and processes" = "Show all apps and processes";
"Show top 8" = "Show top 8";
+"Some of this app's processes have no CPU reading yet, so this total is partial." = "Some of this app's processes have no CPU reading yet, so this total is partial.";
"Some processes started or stopped since the last sample. Their values appear in the next one." = "Some processes started or stopped since the last sample. Their values appear in the next one.";
"Sort by" = "Sort by";
"System processes are not listed. Use Activity Monitor for those." = "System processes are not listed. Use Activity Monitor for those.";
diff --git a/PureMac/pl.lproj/Localizable.strings b/PureMac/pl.lproj/Localizable.strings
index 9770875..4dc0e6b 100644
--- a/PureMac/pl.lproj/Localizable.strings
+++ b/PureMac/pl.lproj/Localizable.strings
@@ -914,6 +914,7 @@
"Settings to review" = "Settings to review";
"Show all apps and processes" = "Show all apps and processes";
"Show top 8" = "Show top 8";
+"Some of this app's processes have no CPU reading yet, so this total is partial." = "Some of this app's processes have no CPU reading yet, so this total is partial.";
"Some processes started or stopped since the last sample. Their values appear in the next one." = "Some processes started or stopped since the last sample. Their values appear in the next one.";
"Sort by" = "Sort by";
"System processes are not listed. Use Activity Monitor for those." = "System processes are not listed. Use Activity Monitor for those.";
diff --git a/PureMac/pt-BR.lproj/Localizable.strings b/PureMac/pt-BR.lproj/Localizable.strings
index 14a8fc3..f30f1cb 100644
--- a/PureMac/pt-BR.lproj/Localizable.strings
+++ b/PureMac/pt-BR.lproj/Localizable.strings
@@ -914,6 +914,7 @@
"Settings to review" = "Settings to review";
"Show all apps and processes" = "Show all apps and processes";
"Show top 8" = "Show top 8";
+"Some of this app's processes have no CPU reading yet, so this total is partial." = "Some of this app's processes have no CPU reading yet, so this total is partial.";
"Some processes started or stopped since the last sample. Their values appear in the next one." = "Some processes started or stopped since the last sample. Their values appear in the next one.";
"Sort by" = "Sort by";
"System processes are not listed. Use Activity Monitor for those." = "System processes are not listed. Use Activity Monitor for those.";
diff --git a/PureMac/ru.lproj/Localizable.strings b/PureMac/ru.lproj/Localizable.strings
index ec277fb..f78e450 100644
--- a/PureMac/ru.lproj/Localizable.strings
+++ b/PureMac/ru.lproj/Localizable.strings
@@ -914,6 +914,7 @@
"Settings to review" = "Settings to review";
"Show all apps and processes" = "Show all apps and processes";
"Show top 8" = "Show top 8";
+"Some of this app's processes have no CPU reading yet, so this total is partial." = "Some of this app's processes have no CPU reading yet, so this total is partial.";
"Some processes started or stopped since the last sample. Their values appear in the next one." = "Some processes started or stopped since the last sample. Their values appear in the next one.";
"Sort by" = "Sort by";
"System processes are not listed. Use Activity Monitor for those." = "System processes are not listed. Use Activity Monitor for those.";
diff --git a/PureMac/uk.lproj/Localizable.strings b/PureMac/uk.lproj/Localizable.strings
index 18521cd..3eddbb7 100644
--- a/PureMac/uk.lproj/Localizable.strings
+++ b/PureMac/uk.lproj/Localizable.strings
@@ -914,6 +914,7 @@
"Settings to review" = "Settings to review";
"Show all apps and processes" = "Show all apps and processes";
"Show top 8" = "Show top 8";
+"Some of this app's processes have no CPU reading yet, so this total is partial." = "Some of this app's processes have no CPU reading yet, so this total is partial.";
"Some processes started or stopped since the last sample. Their values appear in the next one." = "Some processes started or stopped since the last sample. Their values appear in the next one.";
"Sort by" = "Sort by";
"System processes are not listed. Use Activity Monitor for those." = "System processes are not listed. Use Activity Monitor for those.";
diff --git a/PureMac/zh-Hans.lproj/Localizable.strings b/PureMac/zh-Hans.lproj/Localizable.strings
index 247ae14..6d332c1 100644
--- a/PureMac/zh-Hans.lproj/Localizable.strings
+++ b/PureMac/zh-Hans.lproj/Localizable.strings
@@ -913,6 +913,7 @@
"Settings to review" = "Settings to review";
"Show all apps and processes" = "Show all apps and processes";
"Show top 8" = "Show top 8";
+"Some of this app's processes have no CPU reading yet, so this total is partial." = "Some of this app's processes have no CPU reading yet, so this total is partial.";
"Some processes started or stopped since the last sample. Their values appear in the next one." = "Some processes started or stopped since the last sample. Their values appear in the next one.";
"Sort by" = "Sort by";
"System processes are not listed. Use Activity Monitor for those." = "System processes are not listed. Use Activity Monitor for those.";
diff --git a/PureMac/zh-Hant.lproj/Localizable.strings b/PureMac/zh-Hant.lproj/Localizable.strings
index edaa872..bd7347e 100644
--- a/PureMac/zh-Hant.lproj/Localizable.strings
+++ b/PureMac/zh-Hant.lproj/Localizable.strings
@@ -914,6 +914,7 @@
"Settings to review" = "Settings to review";
"Show all apps and processes" = "Show all apps and processes";
"Show top 8" = "Show top 8";
+"Some of this app's processes have no CPU reading yet, so this total is partial." = "Some of this app's processes have no CPU reading yet, so this total is partial.";
"Some processes started or stopped since the last sample. Their values appear in the next one." = "Some processes started or stopped since the last sample. Their values appear in the next one.";
"Sort by" = "Sort by";
"System processes are not listed. Use Activity Monitor for those." = "System processes are not listed. Use Activity Monitor for those.";
diff --git a/PureMacTests/ProcessResourceMonitorTests.swift b/PureMacTests/ProcessResourceMonitorTests.swift
index b68b90d..71533e8 100644
--- a/PureMacTests/ProcessResourceMonitorTests.swift
+++ b/PureMacTests/ProcessResourceMonitorTests.swift
@@ -52,16 +52,24 @@ final class ProcessResourceMonitorTests: XCTestCase {
XCTAssertEqual(app.processCount, 2)
XCTAssertEqual(app.residentBytes, 500)
XCTAssertEqual(try XCTUnwrap(app.cpuFraction), 0.25, accuracy: 0.0001)
+ XCTAssertFalse(app.cpuIsPartial)
}
- func testUnknownPathsRemainDistinctAndNewHelperRequiresWarmup() throws {
+ func testUnknownPathsRemainDistinctAndNewHelperKeepsKnownCPUAsPartial() throws {
let first = reading(path: "")
let second = reading(pid: 43, path: "")
XCTAssertEqual(ProcessResourceMath.consumers(readings: [first, second], previous: [:], processorCount: 8).count, 2)
let before = reading(cpu: 100)
- let result = ProcessResourceMath.consumers(readings: [reading(cpu: 600, time: 2_000_000_000), reading(pid: 43)], previous: [before.identity: before], processorCount: 8)
+ let helper = reading(pid: 43, path: "/Applications/Example.app/Contents/MacOS/Helper", time: 2_000_000_000)
+ let result = ProcessResourceMath.consumers(readings: [reading(cpu: 500_000_100, time: 2_000_000_000), helper], previous: [before.identity: before], processorCount: 4)
XCTAssertEqual(result.count, 1)
- XCTAssertNil(try XCTUnwrap(result.first).cpuFraction)
+ let group = try XCTUnwrap(result.first)
+ XCTAssertEqual(group.processCount, 2)
+ XCTAssertEqual(try XCTUnwrap(group.cpuFraction), 0.125, accuracy: 0.0001, "The measured member still counts")
+ XCTAssertTrue(group.cpuIsPartial)
+ let fresh = ProcessResourceMath.consumers(readings: [helper, reading(time: 2_000_000_000)], previous: [:], processorCount: 4)
+ XCTAssertNil(try XCTUnwrap(fresh.first).cpuFraction, "A group with no measured member stays unknown")
+ XCTAssertTrue(try XCTUnwrap(fresh.first).cpuIsPartial)
}
func testChangedProcessCountSeesStartsAndStopsButNotTheFirstSample() {