Friday 23 July 2021

Python Asyncio Memory leak Or?

I am stuck with the problem of memory usage of my FastAPI application.

If I do some load on my application, e.g. 1 req/sec, for a while, e.g. 2-3 minutes, my application will start use 50+mb more than it was after 1 request. I'm using only one end-point for this test and it does the same every time when I call it (some DB calls using google cloud ndb and it runs it in the executor, some gRPC calls using grpcio, some redis calls using aioredis)

I tried to use all memory profilers (guppy3, tracemalloc, objgraph), all of them say that I have a low number of objects when I stopped loading my application e.g. tracemalloc snapshot first lines

#1: /Users/ndavydov/PycharmProjects/project/.venv/lib/python3.8/site-packages/fastapi/utils.py:65: 2707.8 KiB
    return response_field(field_info=field_info)
#2: /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/linecache.py:137: 1922.0 KiB
    lines = fp.readlines()
#3: /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/abc.py:102: 1643.4 KiB
    return _abc_subclasscheck(cls, subclass)
#4: /Users/ndavydov/PycharmProjects/project/.venv/lib/python3.8/site-packages/fastapi/dependencies/models.py:38: 425.2 KiB
    self.path_params = path_params or []
#5: /Users/ndavydov/PycharmProjects/project/.venv/lib/python3.8/site-packages/fastapi/dependencies/utils.py:755: 310.3 KiB
    BodyModel: Type[BaseModel] = create_model(model_name)
3265 other: 4269.6 KiB
Total allocated size: 14717.7 KiB

But I can see that my application consumes 275 mb, and after 1-2-3 requests before the load, I saw 190-200 mb of memory usage, but here I can see only 14 mb of allocated objects, and this value isn't changed during load time

I was wondering, if you have some ideas about where I can try to find the root of the issue..



from Python Asyncio Memory leak Or?

No comments:

Post a Comment