2

I'm trying to figure out where my memory is being used in a generator function in python 3. I've tried memprof and memory_profiler, and both fail to give me information on generators.

How do I determine where, and how much, memory I am allocating inside a generator function?

0

1 Answer 1

1

The following snippet should work in python3. If you replace the generator function with a list you will see a large difference in memory allocation.

from memory_profiler import memory_usage

print(f'Memory usage after: {memory_usage()}MB')

def obj_generator(num_objects):
    for i in range(num_objects):
        new_obj = {
            'id' : i,
        }
        yield new_obj

objects = obj_generator(1_000_000)

print(f'Memory usage before: {memory_usage()}MB')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.