Skip to content

Commit 27312d7

Browse files
committed
revert to use task_info for process used memory
1 parent 9e2d10e commit 27312d7

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

Common/Cpp/MemoryUtilization/MemoryUtilization_Mac.tpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,31 +93,35 @@ MemoryUsage process_memory_usage(){
9393
}
9494
}
9595
usage.total_system_memory = physical_memory;
96+
{
97+
task_vm_info_data_t tvi;
98+
mach_msg_type_number_t count = TASK_VM_INFO_COUNT;
99+
if(KERN_SUCCESS == task_info(mach_task_self(), TASK_VM_INFO, (task_info_t) &tvi, &count))
100+
{
101+
usage.process_physical_memory = tvi.resident_size; // resident_size = internal + external + reusable
102+
} else {
103+
std::cerr << "Failed to get task info." << std::endl;
104+
}
105+
}
96106
{
97-
mach_vm_address_t address = 0;
98107
mach_vm_size_t size = 0;
99108
vm_region_extended_info_data_t info;
100109
mach_msg_type_number_t count = VM_REGION_EXTENDED_INFO_COUNT;
101110
mach_port_t object_name;
102-
unsigned int pages_resident = 0;
103111
unsigned int pages_swapped_out = 0;
104-
unsigned int pages_dirtied = 0;
105112

106-
while (true) {
113+
for (mach_vm_address_t address = 0;; address += size) {
107114
auto kr = mach_vm_region(mach_task_self(), &address, &size, VM_REGION_EXTENDED_INFO, (vm_region_info_t)&info, &count, &object_name);
108115
if (kr != KERN_SUCCESS) {
109116
if (kr == KERN_INVALID_ADDRESS) { // end of the address space
110117
break;
111118
}
112-
std::cerr << "mach_vm_region_recurse failed with error: " << mach_error_string(kr) << std::endl;
119+
std::cerr << "mach_vm_region failed with error: " << mach_error_string(kr) << std::endl;
113120
break;
114121
}
115-
pages_resident += info.pages_resident;
116122
pages_swapped_out += info.pages_swapped_out;
117-
pages_dirtied += info.pages_dirtied;
118123
address += size;
119124
}
120-
usage.process_physical_memory = (size_t)(pages_resident - pages_dirtied) * (size_t)vm_page_size;
121125
usage.process_virtual_memory = usage.process_physical_memory + (size_t)pages_swapped_out * (size_t)vm_page_size;
122126
}
123127
// ref: https://github.com/htop-dev/htop/blob/main/darwin/Platform.c

0 commit comments

Comments
 (0)