使用 php 获取服务器 RAM
有没有办法用php(使用linux命令的宽度)知道服务器(linux发行版)中的可用内存?
编辑:抱歉,目标是了解特定服务器的服务器/虚拟机中可用的内存(即使该内存是共享的)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您知道此代码只能在 Linux 下运行,则可以使用特殊的
/proc/meminfo
文件来获取有关系统虚拟内存子系统的信息。该文件的格式如下:第一行
MemTotal: ...
包含机器中的物理 RAM 量,减去内核为其自己使用而保留的空间。这是我所知道的获取 Linux 系统上可用内存的简单报告的最佳方法。您应该能够通过类似以下代码的方式提取它:(请注意:此代码可能需要针对您的环境进行一些调整。)
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: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:(Please note: this code may require some tweaking for your environment.)
使用 /proc/meminfo 并将所有内容放入数组中非常简单:
var_dump( getSystemMemInfo() );
Using
/proc/meminfo
and getting everything into an array is simple:var_dump( getSystemMemInfo() );
Linux 命令可以使用 PHP 中的 exec 函数运行。这是有效的并且可以完成工作(如果目标是获得内存)。
尝试以下代码:
Linux commands can be run using the exec function in PHP. This is efficient and will do the job(if objective is to get the memory).
Try the following code:
小而整洁的函数,用于获取与其键关联的所有值。
Small and tidy function to get all of its values associated to their keys.
我认为如果没有专门编写的 PHP 扩展,您就无法访问主机服务器内存信息。 PHP 核心库不允许(可能出于安全原因)访问扩展内存信息。
但是,如果您的脚本可以访问
/proc/meminfo
那么您可以查询该特殊文件并获取您需要的信息。在 Windows 上(尽管您没有要求),我们可以使用com_dotnet
PHP 扩展通过 COM 查询 Windows 框架。您可以在下面找到我的
getSystemMemoryInfo
,无论您是否在 Linux/Windows 服务器上运行脚本,它都会为您返回该信息。wmiWBEMLocatorQuery
只是一个辅助函数。8GB RAM 系统上的示例
输出
如果您想了解每个字段代表什么,那么 了解更多。
I don't think you can access the host server memory info without a special written PHP extension. The PHP core library does not allow (perhaps for security reasons) to access the extended memory info.
However, if your script has access to the
/proc/meminfo
then you can query that special file and grab the info you need. On Windows (although you've not asked for it) we can use thecom_dotnet
PHP extension to query the Windows framework via COM.Below you can find my
getSystemMemoryInfo
that returns that info for you no matter if you run the script on a Linux/Windows server. ThewmiWBemLocatorQuery
is just a helper function.Example on a 8GB RAM system
Output
If you want to understand what each field represent then read more.
exec("grep MemTotal /proc/meminfo", $aryMem);
$aryMem[0] 是您的总内存减去内核使用量。
exec("grep MemTotal /proc/meminfo", $aryMem);
$aryMem[0] has your total ram minus kernel usage.
值得注意的是,在 Windows 中,可以通过执行和解析 shell 命令的输出来获取此信息(以及更多信息):systeminfo
It is worth noting that in Windows this information (and much more) can be acquired by executing and parsing the output of the shell command: systeminfo
我不记得曾经见过这样的函数——实际上,它超出了 PHP 的用途范围。
即使有这样的功能,它也可能以特定于底层操作系统的方式实现,并且可能无法在 Linux 和 Windows 上工作(请参阅
sys_getloadavg
是此类事情的示例)I don't remember having ever seen such a function -- its kind of out the scope of what PHP is made for, actually.
Even if there was such a functionnality, it would probably be implemented in a way that would be specific to the underlying operating system, and wouldn't probably work on both Linux and windows (see
sys_getloadavg
for an example of that kind of thing)// 助手
// helpers