您如何在Ruby中找到计算机上可用的物理内存? [英] How do you find the physical memory free on the machine in Ruby?
问题描述
我想知道系统上有多少物理内存可用,不包括任何交换.有没有一种方法可以在Ruby中获取此信息?
I would like to know how much physical memory is available on the system, excluding any swap. Is there a method to get this information in Ruby?
推荐答案
如果您使用的是Linux,则通常使用"free"命令查找物理内存,即系统上的RAM详细信息
If you are using linux,You usually use a "free" command to find physical memory ie RAM details on the system
output = %x(free)
输出看起来类似于以下字符串
output will look slightly like the following string
" total used free shared buffers cached\nMem: 251308 201500 49808 0 3456 48508\n-/+ buffers/cache: 149536 101772\nSwap: 524284 88612 435672\n"
" total used free shared buffers cached\nMem: 251308 201500 49808 0 3456 48508\n-/+ buffers/cache: 149536 101772\nSwap: 524284 88612 435672\n"
您可以使用简单的字符串操作(如
You can extract the information you need using simple string manipulations like
output.split(" ")[7]
将提供总内存
output.split(" ")[8]
将提供使用的内存
output.split(" ")[9]
将释放可用内存
output.split(" ")[7]
will give total memory
output.split(" ")[8]
will give used memory
output.split(" ")[9]
will give free memory
这篇关于您如何在Ruby中找到计算机上可用的物理内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!