您可以通过以下方式执行此操作exec
:
exec('wmic memorychip get capacity', $totalMemory);
print_r($totalMemory);
这将打印(在我的机器上有 2x2 和 2x4 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 的其他命令行方法可以在
- https://superuser.com/questions/315195/is-there-a-command-to-find-out-the-available-memory-in-windows https://superuser.com/questions/315195/is-there-a-command-to-find-out-the-available-memory-in-windows
另一种编程方式是通过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 示例的详细信息,请参阅:
- http://php.net/manual/en/book.com.php http://php.net/manual/en/book.com.php
- MSDN:构造名字字符串 http://msdn.microsoft.com/en-us/library/windows/desktop/aa389292%28v=vs.85%29.aspx
- MSDN:Win32_ComputerSystem 类 http://msdn.microsoft.com/en-us/library/windows/desktop/aa394102%28v=vs.85%29.aspx
您可能可以从其他 Windows API 获取此信息,例如.NET API。 http://www.php.net/manual/en/book.dotnet.php,还有。
在 Windows 上还有 PECL 扩展可以执行此操作:
- win32_ps_stat_mem — 检索有关全局内存利用率的统计信息。 http://php.net/manual/en/function.win32-ps-stat-mem.php
根据文档,它应该返回一个数组,其中包含(除其他外)名为total_phys
对应于“物理内存总量。"
但由于它是 PECL 扩展,因此您首先必须将其安装在您的计算机上。