1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/python/psutil/examples/free.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,31 @@ 1.4 +#!/usr/bin/env python 1.5 + 1.6 +# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. 1.7 +# Use of this source code is governed by a BSD-style license that can be 1.8 +# found in the LICENSE file. 1.9 + 1.10 +""" 1.11 +A clone of 'free' cmdline utility. 1.12 +""" 1.13 + 1.14 +import psutil 1.15 +from psutil._compat import print_ 1.16 + 1.17 +def main(): 1.18 + virt = psutil.virtual_memory() 1.19 + swap = psutil.swap_memory() 1.20 + templ = "%-7s %10s %10s %10s %10s %10s %10s" 1.21 + print_(templ % ('', 'total', 'used', 'free', 'shared', 'buffers', 'cache')) 1.22 + print_(templ % ('Mem:', int(virt.total / 1024), 1.23 + int(virt.used / 1024), 1.24 + int(virt.free / 1024), 1.25 + int(getattr(virt, 'shared', 0) / 1024), 1.26 + int(getattr(virt, 'buffers', 0) / 1024), 1.27 + int(getattr(virt, 'cached', 0) / 1024))) 1.28 + print_(templ % ('Swap:', int(swap.total / 1024), 1.29 + int(swap.used / 1024), 1.30 + int(swap.free / 1024), 1.31 + '', '', '')) 1.32 + 1.33 +if __name__ == '__main__': 1.34 + main()