top or glances – trust on /proc/meminfo

You may notice that the output of top and glances differs on memory usage (if you are using top and glances the same time). This post will disclose some details about memory info collection by glances and top. And the answer may be known to everyone – /proc/meminfo.

1. top

top is part of procps pkg ( Going deep into the source code, we will find function meminfo() (procps-3.2.8/proc/sysinfo.c) is the one taking care of memory info collection by reading the /proc/meminfo file.

2. glances

Checking the source of glances (, we will find the glances is based on psutil ( However, the memory information processing is a little weird. Instead of totally relying on psutil, glances uses its own computation for certain fields. That is partially because psutil was not mature in the past and glances has to come up with its own hacks. is a code example based on psutil to simulate the current behavior of glances and the latest psutil. Going deep into the psutil (), we could also find that function virtual_memory() is based on reading /proc/meminfo file.

2.1 virtual_memory() (psutil/psutil/

def virtual_memory():
total, free, buffers, shared, _, _ = cext.linux_sysinfo()
cached = active = inactive = None
f = open('/proc/meminfo', 'rb')
CACHED, ACTIVE, INACTIVE = b("Cached:"), b("Active:"), b("Inactive:")
for line in f:


#!/usr/bin/env python

# Copyright (c) 2009, Giampaolo Rodola’. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Added Glances output simulation
# Mar 11, 2014

Print system memory information.

$ python examples/
Total : 9.7G
Available : 4.9G
Percent : 49.0
Used : 8.2G
Free : 1.4G
Active : 5.6G
Inactive : 2.1G
Buffers : 341.2M
Cached : 3.2G

Total : 0B
Used : 0B
Free : 0B
Percent : 0.0
Sin : 0B
Sout : 0B

import psutil
from psutil._compat import print_

def bytes2human(n):
# >>> bytes2human(10000)
# ‘9.8K’
# >>> bytes2human(100001221)
# ‘95.4M’
symbols = (‘K’, ‘M’, ‘G’, ‘T’, ‘P’, ‘E’, ‘Z’, ‘Y’)
prefix = {}
for i, s in enumerate(symbols):
prefix[s] = 1 << (i + 1) * 10 for s in reversed(symbols): if n >= prefix[s]:
value = float(n) / prefix[s]
return ‘%.1f%s’ % (value, s)
return “%sB” % n

def simulate_glances(phymem):
Code below are copied from source code (V 1.7.5)
output = []
# buffers and cached (Linux, BSD)
buffers = getattr(phymem, ‘buffers’, 0)
cached = getattr(phymem, ‘cached’, 0)
# active and inactive not available on Windows
active = getattr(phymem, ‘active’, 0)
inactive = getattr(phymem, ‘inactive’, 0)
# phymem free and usage
total =
free = phymem.available # + buffers + cached
used = total – free
percent = phymem.percent
# Format the output
output.append((‘Total’, total))
output.append((‘Available’, -1))
output.append((‘Percent’, percent))
output.append((‘Used’, used))
output.append((‘Free’, free))
output.append((‘Active’, active))
output.append((‘Inactive’, inactive))
output.append((‘Buffers’, buffers))
output.append((‘Cached’, cached))
for o in output:
value = o[1]
if o[0] != ‘Percent’:
value = bytes2human(value)
print_(‘%-10s : %7s’ % (o[0], value))

def pprint_ntuple(nt):
for name in nt._fields:
value = getattr(nt, name)
if name != ‘percent’:
value = bytes2human(value)
print_(‘%-10s : %7s’ % (name.capitalize(), value))

def main():
#daveti: Hack for glances simulation
psvm = psutil.virtual_memory()

if __name__ == ‘__main__’:

3. summary

top is stable and has not been changed for years; glances is new and based on psutil. According to Nicolas (dev of glances), latest glances is fully leveraging psutil and the current glances will be patched. Meanwhile, cat /proc/meminfo is always there.

About daveti

Interested in kernel hacking, compilers, machine learning and guitars.
This entry was posted in OS and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.