I would like to know how much physical memory is available on the sys开发者_StackOverflowtem, excluding any swap. Is there a method to get this information in Ruby?
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"
You can extract the information you need using simple string manipulations like
output.split(" ")[7]
will give total memory
output.split(" ")[8]
will give used memory
output.split(" ")[9]
will give free memory
Slightly slicker version of AndrewKS's answer:
total_memory_usage_in_k = `ps -Ao rss=`.split.map(&:to_i).inject(&:+)
Well, the Unix command "top" doesn't seem to work in Ruby, so try this:
# In Kilobytes
memory_usages = `ps -A -o rss=`.split("\n")
total_mem_usage = memory_usages.inject { |a, e| a.to_i + e.strip.to_i }
This "seems" correct. I don't guarantee it. Also, this takes a lot more time than the system will so by the time it's finished the physical memory would have changed.
You can use this gem to get various system info http://threez.github.com/ruby-vmstat/
This answers for both, Ruby and Bash and probably also for Python and the rest:
#!/usr/bin/env bash
# This file is in public domain.
# Initial author: martin.vahi@softf1.com
#export S_MEMINFO_FIELD="Inactive"; \
export S_MEMINFO_FIELD="MemTotal"; \
ruby -e "s=%x(cat /proc/meminfo | grep $S_MEMINFO_FIELD: | \
gawk '{gsub(/MemTotal:/,\"\");print}' | \
gawk '{gsub(/kB/,\"*1024\");print}' | \
gawk '{gsub(/KB/,\"*1024\");print}' | \
gawk '{gsub(/KiB/,\"*1024\");print}' | \
gawk '{gsub(/MB/,\"*1048576\");print}' | \
gawk '{gsub(/MiB/,\"*1048576\");print}' | \
gawk '{gsub(/GB/,\"*1073741824\");print}' | \
gawk '{gsub(/GiB/,\"*1073741824\");print}' | \
gawk '{gsub(/TB/,\"*1099511627776\");print}' | \
gawk '{gsub(/TiB/,\"*1099511627776\");print}' | \
gawk '{gsub(/B/,\"*1\");print}' | \
gawk '{gsub(/[^1234567890*]/,\"\");print}' \
); \
s_prod=s.gsub(/[\\s\\n\\r]/,\"\")+\"*1\";\
ar=s_prod.scan(/[\\d]+/);\
i_prod=1;\
ar.each{|s_x| i_prod=i_prod*s_x.to_i};\
print(i_prod.to_s+\" B\")"
The thing to notice here is that the "\" at the ends of the lines are Bash line continuations. Basically it should all be a single-liner, with the exception of the out-commented lines, which must be deleted. The colon at the end of the
grep $S_MEMINFO_FIELD:
is important, because
cat /proc/meminfo | grep Inactive
prints multiple lines, but the rest of the script works with an assumption that the grep outputs only a single line. According to
https://unix.stackexchange.com/questions/263881/convert-meminfo-kb-to-bytes
the /proc/meminfo uses 1024 regardless of whether the unit is "kB" or "KB". I did not have any info about the GiB and GB and TB parts, but I assume that they follow the same style and 1MB=1024*1024KiB.
I created a Linux specific Bash script that takes /proc/meminfo field name as its 1. command line argument and prints out the value in Bytes:
http://longterm.softf1.com/2016/comments/stackoverflow_com/2016_03_07_mmmv_proc_meminfo_filter_t1.bash
(archival copy: https://archive.is/vjcNf )
Thank You for reading my comment. I hope that it helps. :-)
You may use total gem (I'm the author):
require 'total'
puts Total::Mem.new.bytes
精彩评论