2

I'm trying to understand how memory is used in my async Python application. I have multiple coroutines running, and I want to see how much memory each one is using, especially when they are nested or calling each other.

I tried using tracemalloc and asyncio libraries, but I am not sure about correctness.

import asyncio
import tracemalloc

tracemalloc.start()

async def task1():
    print("Task 1 started")
    await asyncio.sleep(1)
    print("Task 1 memory:", tracemalloc.get_traced_memory())



async def task2():
    print("Task 2 started")
    await asyncio.sleep(1)
    print("Task 2 memory:", tracemalloc.get_traced_memory())

async def main():
    await asyncio.gather(task1(), task2())

asyncio.run(main())

This prints some memory numbers, but I don’t know if it is showing memory just for that coroutine or for the whole program. Also, I want to track memory usage over time, not just once at the end.

I need this detail because of strict guidelines about memory usage in my organization.

Is there a way to hook into coroutine lifecycle events or tag them somehow to track memory usage more accurately?

1
  • 1
    There are a few recipes around of instrumenting a recursive function using sys.getsizeof() to get the complete memory used by an object - I've visited a couple of those, and they really don't check inside tasks and co-routines (which can hold data in local variables), thought that is achievable - if I get the time later, I will fix one of those and we should get a nice number. Commented Jun 27 at 13:18

0

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.