Get information about RAM on Mac?

I need to get the total amount of RAM present in the system and the total amount of RAM that is currently in use, so I can calculate the percentage. This is similar to: Get system information on MacOS X?

However, in this question, the best answer tells you how to get RAM by reading from:

/usr/bin/vm_stat 

Due to the nature of my program, I found out that I cannot read from this file - I need a method that will provide me with RAM information without just opening the file and reading from it. I am looking for something to do with function calls. Something like this is preferable: getTotalRam() and getRamInUse() .

I obviously do not expect it to be that simple, but I was looking for a solution other than reading from a file.

I am running Mac OS X Snow Leopard, but it is advisable to get a solution that will work on all modern Mac OS X platforms (like Lion).

Solutions can be in C ++, C or Obj-C, however C ++ will be the best possible solution in my case, so if possible, try providing it in C ++.

+4
source share
4 answers

You should not read / usr / bin / vm _stat, rather you should run it; This program. Look at the first four lines of output

 Pages free: 1880145. Pages active: 49962. Pages inactive: 43609. Pages wired down: 123353. 

Add numbers to the right column and several-sized system pages (as getpagesize () returns), and you will get the total amount of physical memory in the system in bytes.

vm_stat is not installed on Mac OS, so I assume that somewhere there is an unprivileged API to access this information and that vm_stat uses it. But I do not know what this interface is.

+7
source

Getting the device’s physical memory is simple with sysctl :

 int mib [] = { CTL_HW, HW_MEMSIZE }; int64_t value = 0; size_t length = sizeof(value); if(-1 == sysctl(mib, 2, &value, &length, NULL, 0)) // An error occurred // Physical memory is now in value 

VM stats are only a little trickier:

 mach_msg_type_number_t count = HOST_VM_INFO_COUNT; vm_statistics_data_t vmstat; if(KERN_SUCCESS != host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmstat, &count)) // An error occurred 

Then you can use the data in vmstat to get the necessary information:

 double total = vmstat.wire_count + vmstat.active_count + vmstat.inactive_count + vmstat.free_count; double wired = vmstat.wire_count / total; double active = vmstat.active_count / total; double inactive = vmstat.inactive_count / total; double free = vmstat.free_count / total; 

There is also a 64-bit version of the interface.

+9
source

You can find the answer to this question by looking at the source of the top command. You can download the source from http://opensource.apple.com/ . Source 10.7.2 is available as an archive here or in a viewable form here . I recommend downloading the archive and opening top.xcodeproj so you can use Xcode to search for definitions (it is very useful to use commands in Xcode).

The top command displays the physical memory (RAM) numbers after the β€œPhysMem” label. Searching for a project for this line, we find it in the update_physmem function in globalstats.c . It calculates used and free memory numbers from the vm_stat member of the vm_stat structure.

You can click the "vm_stat" command to find its declaration as membor libtop_tsamp_t in libtop.h . It is declared as type vm_statistics_data_t . Clicking a command, proceeding to its definition in /usr/include/mach/vm_statistics.h .

Searching for a project for "vm_stat", we find that it is populated with the libtop_tsamp_update_vm_stats function in libtop.c :

 mach_msg_type_number_t count = sizeof(tsamp->vm_stat) / sizeof(natural_t); kr = host_statistics(libtop_port, HOST_VM_INFO, (host_info_t)&tsamp->vm_stat, &count); if (kr != KERN_SUCCESS) { return kr; } 

You will need to figure out how libtop_port set up if you want to call host_statistics . I am sure you can figure it out for yourself.

+4
source

It's been 4 years, but I just wanted to add more information about calculating the total RAM.

To get shared RAM, we also need to consider Pages occupied by compressor and Pages speculative in addition to Kyle Jones .

You can check this post where the problem occurred.

+2
source

Source: https://habr.com/ru/post/1389964/


All Articles