Skip to content

Instantly share code, notes, and snippets.

@cgbystrom
Created May 21, 2010 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgbystrom/408823 to your computer and use it in GitHub Desktop.
Save cgbystrom/408823 to your computer and use it in GitHub Desktop.
# A simple script to test memory allocation/deallocation in Python
#
# When run under Win32 with Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02)
# [MSC v.1500 32 bit (Intel)]
# it will after each allocation release all memory back the OS.
#
# However, when run under Linux/RHEL5 with
# Python 2.6 (r26:66714, Jun 15 2009, 17:33:43)
# [GCC 4.1.2 20071124 (Red Hat 4.1.2-42)]
# it never releases the memory again, thus eating more and more memory.
#
# Is this really the intended behavior of CPython?
# Or is this due to the underlying memory management of each OS/library?
import gc, time, os, sys
print "PID is", os.getpid()
print "Press Enter to start test"
raw_input()
class X(object):
def __init__(self):
self.foo = None
def ints(n):
print "Allocating %d tuples with 3 integers..." % n
x = [(23, 34, 2523) for i in xrange(n)]
def objects(n):
print "Allocating %d objects..." % n
x = [X() for i in xrange(n)]
size = 1000000
tests = [objects, objects]
for f in tests:
f(size)
print "Done. View process resources with top or similar."
print "Press Enter to continue"
raw_input()
print "Forcing GC collect..."
gc.collect()
print "Press Enter to exit"
raw_input()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment