Last active
September 24, 2019 08:31
-
-
Save georghildebrand/5caf70a2ad59d3a783e325aa30d781c0 to your computer and use it in GitHub Desktop.
Using up some memory with python in a controlled way
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Eat up some memory until 30% of system total | |
import psutil, os | |
nb_process_id = os.getpid() | |
p = psutil.Process(nb_process_id) | |
from multiprocessing import Pool | |
import string | |
import random | |
def f(x): | |
b = [] | |
p = psutil.Process(nb_process_id) | |
letters = string.ascii_lowercase | |
s=''.join(random.choice(letters) for i in range(1000)) | |
while True: | |
memory_usage = p.memory_full_info() | |
# relative memory usage | |
perc = p.memory_percent() | |
if len(b) % 10==0: | |
#print(memory_usage) | |
print("Current used memory space [rss] of the notebook process:", round(memory_usage.rss/2**30,2),'GB') | |
print(perc) | |
if perc < 30: | |
b.append(s*x) | |
else: | |
print("Current used memory space [rss] of the notebook process:", round(memory_usage.rss/2**30,2),'GB') | |
print(perc) | |
break | |
return b | |
_ = f(x=999999) | |
# Switch mprocessing on if needed (take care of pid context) | |
#processes = 12 | |
#pool = Pool(processes = processes) | |
#large_num=512000 | |
#result = pool.map(f, range(large_num,large_num+8)) | |
#pool.close() | |
#pool.terminate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment