Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - measure amount of memory used in script

I have a Python script and I would like to measure the amount of memory that it uses. I have tried memory_profiler but I could not find the right option for me.

I would like something that I can use as:

results = {}
for i in range(10):
    l = [1]*i*1000
    memory = compute_used_memory()
    results[i] = memory

in order to know the amount of memory used during different phases of the program.

EDIT: I do not have much experience but I'll try to explain the problem better. I need to deploy my code on a machine where I will have access only to a limited amount of memory (such as 1 GB). As a consequence I need to know how much memory my code will use. As the code will run incrementally with increasing complexity I need to make sure that the amount of memory used converges to a maximum value. For this reason I need to measure how the amount of memory used changes at different stage of the execution that is in this case represented by a for loop .

like image 911
Donbeo Avatar asked Mar 16 '17 21:03

Donbeo


1 Answers

Hmm, if you are using python3.4 and later you can use the tracemalloc module

import tracemalloc
tracemalloc.start()
profiled_code_here
print("Current: %d, Peak %d" % tracemalloc.get_traced_memory())
like image 91
Giannis Spiliopoulos Avatar answered Oct 05 '22 14:10

Giannis Spiliopoulos