Using PHP, I’d like to get the total memory available to the system (not just the free or used memory).
On Linux it’s quite straight forward. You can do:
$memory = fopen('/proc/meminfo');
and then parse the file.
Is anyone aware of an equivalent method for Windows? I’m open to any suggestions.
Edit: We have a solution (but StackOverflow won’t let me answer my own question):
exec( 'systeminfo', $output );
foreach ( $output as $value ) {
if ( preg_match( '|Total Physical Memory\:([^$]+)|', $value, $m ) ) {
$memory = trim( $m[1] );
}
Not the most elegant solution, and it’s very slow, but it suits my need.
You can do this via
exec:This will print (on my machine having 2×2 and 2×4 bricks of RAM):
You can easily sum this by using
which will then give 12884901888. To turn this into Kilo-, Mega- or Gigabytes, divide by 1024 each, e.g.
Additional command line ways of querying total RAM can be found in
Another programmatic way would be through
COM:For details on this COM example, please see:
You can likely get this information from other Windows APIs, like the .NET API., as well.
There is also PECL extension to do this on Windows:
According to the documentation, it should return an array which contains (among others) a key named
total_physwhich corresponds to “The amount of total physical memory.“But since it’s a PECL extension, you’d first have to install it on your machine.