Skip to content

Commit 610fc2e

Browse files
committed
Memory (macOS): Refines Memory usage detection on macOS to match Activity Monitor more closely
1 parent 716c0e2 commit 610fc2e

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ Features:
44
* Improves compatibility with KDE Plasma 6.5 (#2093, Display)
55
* Adds a `tempSensor` option to specify the sensor name used for CPU temperature detection (CPU)
66
* Example: `{ "type": "cpu", "tempSensor": "hwmon0" /* Use /sys/class/hwmon/hwmon0 for temperature detection */ }`
7+
* Refines Memory usage detection on macOS to match Activity Monitor more closely (Memory, macOS)
78
* Minor optimizations
8-
* Reports usable RAM size to match other platforms (Memory, macOS)
99

1010
Bugfixes:
1111
* Fixes cache line size detection (CPU, macOS)

src/detection/memory/memory_apple.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,25 @@
88
const char* ffDetectMemory(FFMemoryResult* ram)
99
{
1010
size_t length = sizeof(ram->bytesTotal);
11-
if (sysctlbyname("hw.memsize_usable", &ram->bytesTotal, &length, NULL, 0) != 0)
12-
{
13-
if (sysctl((int[]){ CTL_HW, HW_MEMSIZE }, 2, &ram->bytesTotal, &length, NULL, 0) != 0)
14-
return "Failed to read hw.memsize";
15-
}
11+
if (sysctl((int[]){ CTL_HW, HW_MEMSIZE }, 2, &ram->bytesTotal, &length, NULL, 0) != 0)
12+
return "Failed to read hw.memsize";
13+
uint64_t usableMemory = 0;
14+
length = sizeof(usableMemory);
15+
if (sysctlbyname("hw.memsize_usable", &usableMemory, &length, NULL, 0) != 0)
16+
usableMemory = ram->bytesTotal;
1617

1718
mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;
1819
vm_statistics64_data_t vmstat;
1920
if(host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info64_t) (&vmstat), &count) != KERN_SUCCESS)
2021
return "Failed to read host_statistics64";
2122

22-
// https://github.com/exelban/stats/blob/master/Modules/RAM/readers.swift#L56
23-
ram->bytesUsed = ((uint64_t)
24-
+ vmstat.active_count
25-
+ vmstat.inactive_count
26-
+ vmstat.speculative_count
27-
+ vmstat.wire_count
28-
+ vmstat.compressor_page_count
29-
- vmstat.purgeable_count
30-
- vmstat.external_page_count
31-
) * instance.state.platform.sysinfo.pageSize;
23+
uint64_t pageSize = instance.state.platform.sysinfo.pageSize;
24+
uint64_t appMemory = vmstat.internal_page_count * pageSize;
25+
uint64_t wiredMemory = vmstat.wire_count * pageSize;
26+
uint64_t compressed = vmstat.compressor_page_count * pageSize;
27+
uint64_t reserved = ram->bytesTotal - usableMemory;
28+
29+
ram->bytesUsed = appMemory + wiredMemory + compressed + reserved;
3230

3331
return NULL;
3432
}

0 commit comments

Comments
 (0)