用php获取服务器ram [英] get server ram with php
问题描述
有没有办法使用php(使用linux命令进行widthout)来了解服务器(Linux发行版)中可用的ram?
Is there a way to know the avaliable ram in a server (linux distro) with php (widthout using linux commands)?
抱歉,目的是要知道服务器/虚拟机中特定服务器可用的内存(即使该内存是共享的).
edit: sorry, the objective is to be aware of the ram available in the server / virtual machine, for the particular server (even if that memory is shared).
推荐答案
如果您知道此代码仅在Linux下运行,则可以使用特殊的/proc/meminfo
文件来获取有关系统的虚拟内存子系统的信息.该文件的格式如下:
If you know this code will only be running under Linux, you can use the special /proc/meminfo
file to get information about the system's virtual memory subsystem. The file has a form like this:
MemTotal: 255908 kB
MemFree: 69936 kB
Buffers: 15812 kB
Cached: 115124 kB
SwapCached: 0 kB
Active: 92700 kB
Inactive: 63792 kB
...
第一行MemTotal: ...
包含计算机中的物理RAM量,减去内核保留供自己使用的空间.这是获得Linux系统上可用内存的简单报告的最佳方法.您应该可以通过以下代码将其提取:
That first line, MemTotal: ...
, contains the amount of physical RAM in the machine, minus the space reserved by the kernel for its own use. It's the best way I know of to get a simple report of the usable memory on a Linux system. You should be able to extract it via something like the following code:
<?php
$fh = fopen('/proc/meminfo','r');
$mem = 0;
while ($line = fgets($fh)) {
$pieces = array();
if (preg_match('/^MemTotal:\s+(\d+)\skB$/', $line, $pieces)) {
$mem = $pieces[1];
break;
}
}
fclose($fh);
echo "$mem kB RAM found"; ?>
(请注意:此代码可能需要针对您的环境进行一些调整.)
(Please note: this code may require some tweaking for your environment.)
这篇关于用php获取服务器ram的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!