@@ -94,6 +94,7 @@ MemoryUsage process_memory_usage(){
9494 std::cerr << " Error getting process info for PID " << pid << " : " << strerror (errno) << std::endl;
9595 }else {
9696 usage.process_physical_memory = task_info.pti_resident_size ;
97+ usage.process_virtual_memory = task_info.pti_virtual_size ;
9798 }
9899
99100 int mib[] = {CTL_HW, HW_MEMSIZE};
@@ -106,25 +107,16 @@ MemoryUsage process_memory_usage(){
106107 usage.total_system_memory = physical_memory;
107108 }
108109
109- vm_size_t page_size;
110110 vm_statistics_data_t vm_stats;
111- mach_port_t mach_port = mach_host_self ();
112- mach_msg_type_number_t count = sizeof (vm_stats) / sizeof (integer_t );
113-
114- // Get the host statistics
115- if (KERN_SUCCESS != host_statistics (mach_port, HOST_VM_INFO, (host_info_t )&vm_stats, &count)) {
111+ mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
112+ if (host_statistics (mach_host_self (), HOST_VM_INFO, (host_info_t )&vm_stats, &count) != 0 ) {
116113 std::cerr << " Failed to get host statistics." << std::endl;
117- } else {
118- // Get the system's page size
119- host_page_size (mach_port, &page_size);
120-
114+ } else {
121115 // Calculate used memory from vm_statistics
122- // Used memory = Wired + Active + Inactive
123- size_t wired_pages = vm_stats.wire_count ;
124- size_t active_pages = vm_stats.active_count ;
125- size_t inactive_pages = vm_stats.inactive_count ;
126-
127- usage.total_used_system_memory = (wired_pages + active_pages + inactive_pages) * page_size;
116+ // Used = active_count + wire_count
117+ // Cached = inactive_count
118+ // Buffered = purgeable_count
119+ usage.total_used_system_memory = (size_t )(vm_stats.active_count + vm_stats.wire_count ) * (size_t )vm_page_size;
128120 }
129121 return usage;
130122}
0 commit comments