Does my web app have memory leak (Postgresql-13)?

Viewed 32

I use Postgresql-13 in my ASP.NET Core 6 MVC project, I suspect I had memory leak in my web app, since I got memory exhausted exception below.

Questions

  • Just want to double check, does my insert/update and query was properly close the connection?
  • Does the using command always automatically close the connection and clear the pool?
  • Did I miss something in my connection string?
  • Do I need to always call NpgsqlConnection.ClearPool()?

My connection string:

"Server=localhost;Database=websales;User ID=xxx;Password=xxx;Keepalive=30; Connection 
Idle Lifetime=300; Tcp Keepalive=true";

My insert/update was something like this:

        try
        {
            using (NpgsqlConnection conn = new 
             NpgsqlConnection(Models.AppSettings.PG_SQL.Connection_String))
            {
                try
                {
                    conn.Execute(sqlInsertUpdate, new
                    {
                        Tokens_Pid = tokensPid,
                    });
                }
                catch (Exception e) { }
                finally { }
            }
        }
        catch (Exception e) { }
        finally { }

My query command was something like this:

        try
        {
            using (NpgsqlConnection conn = new 
            NpgsqlConnection(Models.AppSettings.PG_SQL.Connection_String))
            {
                try
                {
                    using (var dr = conn.ExecuteReader(sql, new
                    {
                        Crons_Pid = cronsPid
                    }))
                    {
                        if (dr.Read())
                        {
                            // some processes
                        }
                    }
                }
                catch (Exception e) { }
                finally{ }
            }
        }
        catch (Exception e) {  }
        finally { }

This is my config:

/etc/sysctl.conf
kernel.shmmax=100663296

/var/lib/pgsql/13/data/postgresql.conf
max_connection=100
shared_buffer=512

This is the exception I get:

Npgsql.NpgsqlException (0x80004005): The connection pool has been exhausted, either raise 'Max Pool Size' (currently 100) or 'Timeout' (currently 15 seconds) in your connection string.

System.TimeoutException: The operation has timed out.

at Npgsql.ConnectorPool.g__RentAsync|28_0(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlConnection.g__OpenAsync|45_0(Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlConnection.Open()
at Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, CommandDefinition& command, Action2 paramReader) in /_/Dapper/SqlMapper.cs:line 2858 at Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, CommandDefinition& command) in /_/Dapper/SqlMapper.cs:line 581 at Dapper.SqlMapper.Execute(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable1 commandTimeout, Nullable`1 commandType) in /_/Dapper/SqlMapper.cs:line 452

Any help is very appreciated.

many thanks in advance Don

0 Answers
Related