在Windows上使用PHP获取全部可用的系统内存

使用PHP,我想获得系统可用的总内存(不只是免费或使用的内存)。

在Linux上,它非常简单。 你可以做:

$memory = fopen('/proc/meminfo');

然后parsing文件。

有谁知道Windows的等效方法? 我接受任何build议。

编辑:我们有一个解决scheme(但StackOverflow不会让我回答我自己的问题):

 exec( 'systeminfo', $output ); foreach ( $output as $value ) { if ( preg_match( '|Total Physical Memory\:([^$]+)|', $value, $m ) ) { $memory = trim( $m[1] ); } 

不是最优雅的解决scheme,它非常慢,但它适合我的需要。

你可以通过exec来做到这一点:

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

这将打印(在我的机器上有2×2和2×4砖的RAM):

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

你可以很容易地通过使用

 echo array_sum($totalMemory); 

然后给出12884901888.要把它变成千位,兆位或千兆字节,除以1024每个,例如

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

查询总RAM的其他命令行方式可以在中找到


另一个编程方式是通过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 ); 

有关此COM示例的详细信息,请参阅:

您可能会从其他Windows API(如.NET API)获取此信息。 也是如此。


Windows上也有PECL扩展:

  • win32_ps_stat_mem – 检索关于全局内存利用率的统计信息。

根据文档,它应该返回一个数组,其中包含(除其他之外)名为total_phys对应于“ 总物理内存量

但是由于这是一个PECL扩展,你首先必须在你的机器上安装它。

这是一个次要的(可能更适合超级用户)的区别,但是由于它是在最近的Windows服务中为我提供的,我将在这里提供它。 问题是关于可用内存,而不是全部物理内存。

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