Re: get_used_memory

Paul Rubin <[email protected]> Sat, 13 Jun 2026 14:13:40 -0700
Newsgroups comp.lang.python
Organization A noiseless patient Spider
Message-ID <[email protected]>
Jon Ribbens <[email protected]> writes:

> Oh, and doesn't have ints as the dictionary values, which is rather
> more fatal to the use-case.

> print("Used memory: %d Kb" % (info["MemTotal"] - info["MemFree"] - info["Buffers"] - info["Cached"] + info["SwapTotal"] - info["SwapFree"] - info["SwapCached"]))

It really should look at the units in the meminfo file too, but anyway,

alloced = ['MemTotal', 'SwapTotal']
free = ['MemFree, 'Buffers', 'Cached', 'SwapFree', 'SwapCached']

def total(xs): return sum(int(info[f'{x}:']) for x in xs)
print(f''Used memory: {total(alloced) - total(free)} Kb')