Skip to content

Memory leak with Pool usage #1024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
timberkerkvliet opened this issue Apr 19, 2023 · 3 comments
Open

Memory leak with Pool usage #1024

timberkerkvliet opened this issue Apr 19, 2023 · 3 comments

Comments

@timberkerkvliet
Copy link

timberkerkvliet commented Apr 19, 2023

  • asyncpg version: 0.27
  • PostgreSQL version: 14
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
    the issue with a local PostgreSQL install?
    : no
  • Python version: 3.8, 3.10 and 3.11
  • Platform: docker images python:3.x-slim-buster
  • Do you use pgbouncer?: no
  • Did you install asyncpg with pip?: yes
  • Can the issue be reproduced under both asyncio and
    uvloop?
    : only asyncio

When we use a Pool, the memory usage of our process keeps increasing slowly with the number of queries. After 1000 queries, it is ~10MB and this increases with the number of queries executed on connections acquired from the pool. This remains after the max_inactive_connection_lifetime has passed with no activity, with explicit garbage collection and even after we close the pool. We can reproduce this with a psutil measurement and observe it in production. When we replace the usage of Pool with our own creation/cleanup of asynpg Connections, the issue dissapears.

Pool created with the following arguments:

asyncpg.create_pool(
    max_inactive_connection_lifetime=300,
    command_timeout=30,
    min_size=0,
    max_size=5
)
@elprans
Copy link
Member

elprans commented Aug 15, 2023

Possibly #955?

@stollero
Copy link

stollero commented Nov 6, 2024

Hi there,

  • asyncpg version: 0.30.0
  • PostgreSQL version: 16
  • Do you use a PostgreSQL SaaS? If so, which? Goggle Cloud SQL
    the issue with a local PostgreSQL install?
    : yes
  • Python version: 3.12.7
  • Platform: Docker image Python 3.12.7
  • Do you use pgbouncer?: No
  • Did you install asyncpg with pip?: yes
  • If you built asyncpg locally, which version of Cython did you use?: -
  • Can the issue be reproduced under both asyncio and
    uvloop?
    : used with uvicorn

I created a repository to reproduce the memoryleak we experience too. With every request the memory steadily increases. We use asyncpg in combination with sqlalchemy. We did not observe an increase of connections to the database, so connection pool reuses connections but leaks memory somewhere else.

https://github.com/stollero/async-alchemy-memoryleak

leak

@circlingthesun
Copy link

I appear to have the same problem. I ran tracemalloc in production and this line stood out:

/usr/local/lib/python3.13/site-packages/asyncpg/connection.py:470: size=916 KiB (+916 KiB), count=14302 (+14302), average=66 B

14302 connection objects seems a bit excessive .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants