[Python] How can I dynamically trace and visualize memory allocation patterns of nested Python...

Discussão em 'Python' iniciado por Stack, Junho 26, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página