Skip to content

Commit

Permalink
Fix memory reporting for linux systems
Browse files Browse the repository at this point in the history
/proc/meminfo reports memory in KiloBytes and so needs a multiplier of
1024 instead of 1000.
The kernel reports in terms of pages and the proc filesystem is left
shifting by 2 for 4KB pages to get KB. Since this is a binary shift,
Bytes will need to shift by 10 and so get multiplied by 1024.

From the kernel code. PAGE_SHIFT = 12 for 4KB pages
"MemTotal:       %8lu kB\n", K(i.totalram)

Thanks to @subhachandrachandra!
  • Loading branch information
sparrc committed Aug 27, 2015
1 parent 0d7ff2e commit ce70817
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions mem/mem_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ func VirtualMemory() (*VirtualMemoryStat, error) {
}
switch key {
case "MemTotal":
ret.Total = t * 1000
ret.Total = t * 1024
case "MemFree":
ret.Free = t * 1000
ret.Free = t * 1024
case "Buffers":
ret.Buffers = t * 1000
ret.Buffers = t * 1024
case "Cached":
ret.Cached = t * 1000
ret.Cached = t * 1024
case "Active":
ret.Active = t * 1000
ret.Active = t * 1024
case "Inactive":
ret.Inactive = t * 1000
ret.Inactive = t * 1024
}
}
ret.Available = ret.Free + ret.Buffers + ret.Cached
Expand Down

0 comments on commit ce70817

Please sign in to comment.