Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get total available system memory with PHP on Windows

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.

like image 620
John Blackbourn Avatar asked Aug 30 '11 20:08

John Blackbourn


2 Answers

You can do this via exec:

exec('wmic memorychip get capacity', $totalMemory);
print_r($totalMemory);

This will print (on my machine having 2x2 and 2x4 bricks of RAM):

Array
(
    [0] => Capacity
    [1] => 4294967296
    [2] => 2147483648
    [3] => 4294967296
    [4] => 2147483648
    [5] =>
)

You can easily sum this by using

echo array_sum($totalMemory);

which will then give 12884901888. To turn this into Kilo-, Mega- or Gigabytes, divide by 1024 each, e.g.

echo array_sum($totalMemory) / 1024 / 1024 / 1024; // GB

Additional command line ways of querying total RAM can be found in

  • https://superuser.com/questions/315195/is-there-a-command-to-find-out-the-available-memory-in-windows

Another programmatic way would be through COM:

// connect to WMI
$wmi = new COM('WinMgmts:root/cimv2');

// Query this Computer for Total Physical RAM
$res = $wmi->ExecQuery('Select TotalPhysicalMemory from Win32_ComputerSystem');

// Fetch the first item from the results
$system = $res->ItemIndex(0);

// print the Total Physical RAM
printf(
    'Physical Memory: %d MB', 
    $system->TotalPhysicalMemory / 1024 /1024
);

For details on this COM example, please see:

  • http://php.net/manual/en/book.com.php
  • MSDN: Constructing a Moniker String
  • MSDN: Win32_ComputerSystem class

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:

  • win32_ps_stat_mem — Retrieves statistics about the global memory utilization.

According to the documentation, it should return an array which contains (among others) a key named total_phys which 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.

like image 197
Gordon Avatar answered Oct 14 '22 01:10

Gordon


This is a minor (and possibly more suitable for SuperUser) distinction, but as it's come up for me in a recent windows service, I'll provide it here. The question asks about available memory, not total physical memory.

exec('wmic OS get FreePhysicalMemory /Value 2>&1', $output, $return);
$memory = substr($output[2],19);

echo $memory;
like image 28
Daniel Ingraham Avatar answered Oct 14 '22 03:10

Daniel Ingraham