Skip to content

Instantly share code, notes, and snippets.

@9thbit
Created December 14, 2013 11:47
Show Gist options
  • Save 9thbit/7958266 to your computer and use it in GitHub Desktop.
Save 9thbit/7958266 to your computer and use it in GitHub Desktop.
Chews up CPU and RAM. Fills the current available memory with random integers and repeatedly sorts and shuffles them. Was used to stress test some RAM modules.
from multiprocessing import Pool, cpu_count
import random
import time
import sys
import os
import re
MEMINFO = "/proc/meminfo"
def getfreemem():
with open(MEMINFO, "rt") as f:
for line in f:
if line.startswith("MemFree"):
print line
bits = [a.strip() for a in line.split(" ") if len(a.strip())]
return int(bits[-2]) * 250 # MemFree is in kb
def chew(num_bytes):
a = []
while sys.getsizeof(a) < num_bytes and getfreemem:
a.append(random.random())
print "Chew:", len(a), sys.getsizeof(a), num_bytes
while True:
random.shuffle(a)
a.sort()
def nomnomnom():
num_cpus = cpu_count()
pool = Pool(processes=num_cpus)
free_mem = getfreemem()
print "Free mem:", free_mem
for i in xrange(num_cpus):
pool.apply_async(chew, args=[free_mem / num_cpus])
pool.close()
pool.join()
if __name__ == '__main__':
nomnomnom()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment