Method: LinuxStat::Memory.stat

Defined in:
lib/linux_stat/memory.rb

.statObject

Returns the memory details reported by /proc/meminfo. In this format:

{:total=>3836264, :used=>3097952, :available=>738312, :percent_used=>80.75, :percent_available=>19.25}

The values are in Kilobyte.

If the statistics is not available, it will return an empty Hash.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/linux_stat/memory.rb', line 13

def stat
	return {} unless meminfo?

	memory = IO.foreach('/proc/meminfo'.freeze).first(3)

	total = memory[0].split[1].to_i
	available = memory[2].split[1].to_i
	used = total - available

	percent_used = total == 0 ? 0.0 : used.*(100).fdiv(total).round(2)
	percent_available = total == 0 ? 0.0 : available.*(100).fdiv(total).round(2)

	# We have all the methods, but each methods reads the same file
	#
	# So better to use the above calculation
	{
		total: total,
		used: used,
		available: available,
		percent_used: percent_used,
		percent_available: percent_available
	}
end