Donbeo
Donbeo

Reputation: 17647

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 .

Upvotes: 7

Views: 3106

Answers (2)

Giannis Spiliopoulos
Giannis Spiliopoulos

Reputation: 2698

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())

Upvotes: 9

Piero Marini
Piero Marini

Reputation: 131

Im guessing Python3 so try with the "Resource" module. The getrusage function should give you what you need.

Start here

example EDIT:

import resource
for i in range(10):
    l = [1] * i * 1000
    memory = resource.getrusage(resource.RUSAGE_SELF)
    print(memory.ru_utime)

The function returns an object with various properties which you can access (check the docs for every one of them).

Upvotes: -1

Related Questions