7

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 .

1
  • This is kind of a tricky question, and there are a few ways you could interpret it in terms of allocated to utilized memory. Do you want total allocated memory of the program? Do you want a percentage of used to allocated memory? Do you want total memory allocated at a particular point in the program? A little more clarification may help. Commented Mar 16, 2017 at 21:38

2 Answers 2

9

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())
Sign up to request clarification or add additional context in comments.

Comments

-1

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

6 Comments

I actually need for python 2 but something for python 3 is a good start
cna you please write an example usage?
Editing answer just a sec
Which one is the total amount of used memory among the values that it returns?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.