Skip to content

Commit 7d74c9f

Browse files
committed
Fixes total_used_system_memory
1 parent c90da2e commit 7d74c9f

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

Common/Cpp/MemoryUtilization/MemoryUtilization_Mac.tpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,31 @@ MemoryUsage process_memory_usage(){
120120
usage.process_physical_memory = (size_t)(pages_resident - pages_dirtied) * (size_t)vm_page_size;
121121
usage.process_virtual_memory = usage.process_physical_memory + (size_t)pages_swapped_out * (size_t)vm_page_size;
122122
}
123+
// ref: https://github.com/htop-dev/htop/blob/main/darwin/Platform.c
124+
#ifdef __arm64__
125+
{
126+
vm_statistics64_data_t vm_stats;
127+
mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;
128+
if (KERN_SUCCESS == host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info_t)&vm_stats, &count)) {
129+
auto used = vm_stats.active_count + vm_stats.inactive_count +
130+
vm_stats.speculative_count + vm_stats.wire_count + vm_stats.compressor_page_count
131+
- vm_stats.purgeable_count - vm_stats.external_page_count;
132+
usage.total_used_system_memory = (size_t)used * (size_t)vm_page_size;
133+
} else {
134+
std::cerr << "Failed to get host statistics64." << std::endl;
135+
}
136+
}
137+
#else
123138
{
124139
vm_statistics_data_t vm_stats;
125140
mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
126141
if (KERN_SUCCESS == host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vm_stats, &count)) {
127-
// Calculate used memory from vm_statistics
128-
// Used = active_count + wire_count
129-
// Cached = inactive_count
130-
// Buffered = purgeable_count
131142
usage.total_used_system_memory = (size_t)(vm_stats.active_count + vm_stats.wire_count) * (size_t)vm_page_size;
132143
} else {
133144
std::cerr << "Failed to get host statistics." << std::endl;
134145
}
135146
}
147+
#endif
136148
return usage;
137149
}
138150

0 commit comments

Comments
 (0)