在 Windows 上使用 PHP 获取总可用系统内存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7249138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:58:02  来源:igfitidea点击:

Get total available system memory with PHP on Windows

phpwindowsmemoryram

提问by John Blackbourn

Using PHP, I'd like to get the total memory available to the system (not just the free or used memory).

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

On Linux it's quite straight forward. You can do:

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

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

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

and then parse the file.

然后解析文件。

Is anyone aware of an equivalent method for Windows? I'm open to any suggestions.

有没有人知道 Windows 的等效方法?我愿意接受任何建议。

Edit: We have a solution (but StackOverflow won't let me answer my own question):

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

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.

不是最优雅的解决方案,而且速度很慢,但它适合我的需要。

采纳答案by Gordon

You can do this via exec:

您可以通过exec以下方式执行此操作:

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

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

这将打印(在我的机器上有 2x2 和 2x4 块内存):

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.

然后将给出 12884901888。要将其转换为 Kilo-、Mega- 或 Gigabytes,每个除以 1024,例如

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

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

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



Another programmatic way would be through COM:

另一种编程方式是通过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:

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

You can likely get this information from other Windows APIs, like the .NET API., as well.

您可能会从其他 Windows API(例如.NET API)中获取此信息,同样。



There is also PECL extension to do this on Windows:

在 Windows 上还有一个 PECL 扩展可以做到这一点:

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."

根据文档,它应该返回其含有(等等)命名的键的阵列total_phys,其对应于“总物理存储器的量。

But since it's a PECL extension, you'd first have to install it on your machine.

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

回答by Daniel Ingraham

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 availablememory, not total physical memory.

这是一个次要的(可能更适合超级用户)区别,但由于它是在最近的 Windows 服务中出现的,我将在此处提供。该问题询问可用内存,而不是总物理内存。

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

echo $memory;