import gc def memory(): import os from wmi import WMI w = WMI('.') result = w.query("SELECT WorkingSet FROM Win32_PerfRawData_PerfProc_Process WHERE IDProcess=%d" % os.getpid()) res = int(result[0].WorkingSet) return res / 1048576.0 def test(): BIG_NUM = 10000000 print memory() a = range(BIG_NUM) print 'in test', memory() def f(): b = range(BIG_NUM) print 'in f', memory() f() print 'after f', memory() f() print 'after f', memory() print gc.collect() print memory() test() print memory() gc.collect() print memory()