I’m working on a system that was developed for linux and that needs the Total Memory of the machine and the Free Memory.
For that, the system used the /proc/ folder of linux to get that data, but mac doesn’t have that folder.
Two days ago I got bored of running a virtual machine on my computer only to be able to test and started making the program compatible with mac.
Because of that I started evaluating different methods, but none worked.
The most popular answers that you can see on internet if you search for this problem are the following:
- free (-bash: free: command not found // It doesn’t exist in mac).
- Top (I was able to look at the info in the console, but I wasn’t able to retrieve it from the code).
But because of the reasons I added between the parenthesis, they are not useful.
How can I get the Total Memory and the Free Memory on a mac computer so that I can use it from a program?
NOTE: I am working on php. If you have a php solution, it will work, but I prefer a general solution.
Here are bunch of answers from apple.stackexchange.com from an exact duplicate of this question, wasn’t hard to find from a simple Google search.
The Python solution that uses
psandvmstatcould easily be re-written directly in PHP in just about the same amount of lines of code.There are many solutions included, all the command line ones have outputs that can be parsed.
All the Python and Ruby solutions pretty much explain in plain simple code how to use
sysctland parse it’s output to get the free memory. Most of them are single functions or even one liners.Here is a list of all the Unix command line tools that ship with OSX, you might find something more platform specific, but the
freework alike scripts in the above link would be more platform agnostic.