threez/ruby-vmstat

why the @memory.free it not equal the free command result?

Closed this issue · 5 comments

Hi, i run free on ubuntu, it's report the free memory is 291M. but vmstat report it 72.046M, is it problem?

free -m
             total       used       free     shared    buffers     cached
Mem:          3953       3661        291          0        435        874
-/+ buffers/cache:       2351       1602
Swap:         4093        505       3588
2.0.0-p353 :001 > require 'vmstat'
 => true 
2.0.0-p353 :002 > Vmstat.snapshot
 => #<Vmstat::Snapshot:0x00000002416f98 @at=2014-04-25 10:54:43 +0800, @boot_time=2014-03-21 18:20:31 +0800, @cpus=[#<struct Vmstat::Cpu num=0, user=1505724, system=428605, nice=18475, idle=296399959>, #<struct Vmstat::Cpu num=1, user=1408257, system=367915, nice=11805, idle=296995241>, #<struct Vmstat::Cpu num=2, user=1241444, system=317692, nice=14167, idle=297237353>, #<struct Vmstat::Cpu num=3, user=1349585, system=342413, nice=10496, idle=297150395>], @disks=[#<struct Vmstat::LinuxDisk type=:ext4, origin="/", mount="/", block_size=4096, free_blocks=245113613, available_blocks=231740186, total_blocks=263158381>], @load_average=#<struct Vmstat::LoadAverage one_minute=0.02, five_minutes=0.05, fifteen_minutes=0.05>, @memory=#<struct Vmstat::Memory pagesize=4096, wired=221782, active=375840, inactive=342435, free=72046, pageins=58366033, pageouts=167523232>, @network_interfaces=[#<struct Vmstat::NetworkInterface name=:eth0, in_bytes=18760131266, in_errors=0, in_drops=988, out_bytes=11677543487, out_errors=0, type=6>, #<struct Vmstat::NetworkInterface name=:lo, in_bytes=8794265684, in_errors=0, in_drops=0, out_bytes=8794265684, out_errors=0, type=24>], @task=#<struct Vmstat::Task virtual_size=12986, resident_size=3403, user_time_ms=18000, system_time_ms=4000>> 
2.0.0-p353 :003 > Vmstat.memory.free
 => 72046

Free counts different. The ruby method returns the real numbers as returned by the OS. Like:

Vmstat.snapshot.memory #=> #<struct Vmstat::Memory pagesize=4096, wired=127899, active=460135, inactive=370650, free=39356, pageins=233111829, pageouts=74733982>

The program free counts simplified. Look at the number inactive. Maybe you want inactive + free = free and wired + active = used.

thanks.

I've run into a similar issue. Is there a way to calculate the amount of memory available to the processes including SWAP?

             total       used       free     shared    buffers     cached
Mem:      65950944   53662188   12288756     643036    1732784   34322936
-/+ buffers/cache:   17606468   48344476
Swap:            0          0          0

For example, I'd like to get the 48344476 number as reported by free -- not sure how SWAP figures into this, as I haven't got any, but I'd like for it to be included.

Also, may I suggest adding some helper methods to handle those common calculations?

Cheers

As I said earlier in the thread:

The program free counts simplified. Look at the number inactive. Maybe you want inactive + free = free and wired + active = used.

The program free is not on all OSs, in fact i think it is only on linux. I would not like to add OS specific calculations, that might be meaningless on other OSs.

That's fair enough, although I don't think that you can get the available memory from the exposed data due to the kernel buffers/cache -- which is technically in use, but can be reallocated to whatever needs it.