Skip to content

Instantly share code, notes, and snippets.

@dkilcy
Created February 6, 2015 00:29
Show Gist options
  • Save dkilcy/cee5a37398d0853cf186 to your computer and use it in GitHub Desktop.
Save dkilcy/cee5a37398d0853cf186 to your computer and use it in GitHub Desktop.
Program to eat memory and exit
import os
import psutil
PROCESS = psutil.Process(os.getpid())
MEGA = 10 ** 6
MEGA_STR = ' ' * MEGA
def pmem():
#tot, avail, percent, used, free = psutil.virtual_memory()
svmem = psutil.virtual_memory()
tot, avail, used, free = svmem.total / MEGA, svmem.available / MEGA, svmem.used / MEGA, svmem.free / MEGA
proc = PROCESS.get_memory_info()[1] / MEGA
print('process = %s total = %s avail = %s used = %s free = %s percent = %s'
% (proc, tot, avail, used, free, svmem.percent))
def alloc_max_array():
i = 0
ar = []
while True:
try:
#ar.append(MEGA_STR) # no copy if reusing the same string!
ar.append(MEGA_STR + str(i))
except MemoryError:
break
i += 1
max_i = i - 1
print 'maximum array allocation:', max_i
pmem()
def alloc_max_str():
i = 0
while True:
try:
a = ' ' * (i * 10 * MEGA)
del a
except MemoryError:
break
i += 1
max_i = i - 1
_ = ' ' * (max_i * 10 * MEGA)
print 'maximum string allocation', max_i
pmem()
pmem()
alloc_max_str()
alloc_max_array()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment