1

We are getting a severe memory leak in our C# application. We insulated all the possible suspects and found that the data layer - SqlConnection - causing the memory leak. We are executing many SqlConnection requests. Approximately 10 requests per second. And we are getting a leak of 200M per day.

We are using the following SqlConnection access as base class:

public class Access : IDisposable
{
    protected SqlConnection connection;

    private readonly object locker = new object();

    protected void OpenConnection()
    {
        if (this.connection == null)
        {
            lock (locker)
            {
                if (this.connection == null)
                {
                    string conn = Config.Data["data"]["mainConnString"];
                    this.connection = new SqlConnection(conn);
                    this.connection.Open();
                }
            }
        }
        else if (this.connection.State == System.Data.ConnectionState.Closed)
        {
            lock (locker)
            {
                if (this.connection.State == System.Data.ConnectionState.Closed)
                {
                    this.connection.Open();
                }
            }
        }
    }

    public void Dispose()
    {
        if (this.connection != null)
        {
            this.connection.Close();
            this.connection.Dispose();
        }
    }
}

All our SqlConnection classes inherit from this base class above, and we surround each SqlConnection usage with a using block

So for instance if we have a Product table and a ProductAccess the inherits the Access class.

We are using the following code:

using (var acceess = new ProductAccess ())
{
    Product product = acceess .GetProductById (id);
}

Therefore each time we use SqlConnection we create a new one and after we finish using it we dispose it (because the usage is surrounded by using block)

We are using c# v4.0.30319 and Microsoft SQL Server 2008 R2.

Anyone knows or have any ideas why this memory leak occurred?

Maybe it is related to garbage collection issue or garbage collection configuration?

Maybe this leak related to GC.SuppressFinalize(this) method call that the SqlConnection dispose() method call?

Thanks

1
  • Did you ever find the solution to this? Commented Jan 7, 2022 at 15:45

1 Answer 1

1

I encountered this problem several years ago. I realized it was about Connection Pool. As MSDN says:

"Connection pooling reduces the number of times that new connections must be opened. The pooler maintains ownership of the physical connection. It manages connections by keeping alive a set of active connections for each given connection configuration."

You can either clear the pool after a connection is closed (heavy and inefficient), or you can set the Min Pool SizeandMax Pool Size in your connection string by try and error to optimal values. I believe in your case tuning Max Pool Size is much more important. Try to reduce it and watch the result. It is good to start with Min and Max value=1 and test; although this may harm the performance. I hope this helps.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks, i will check if adding Max Pool Size property to the connection string fix that.
Please report back. I'm as curious as you
didn't fix the leak. i think that the leak is related to garbage collection issue , we saw that the leak caused by unmanaged resources (discovered by using c# ANTS Performance Profiler). any other suggestions to fix the leak?
@user2456342 Do you use any unmanaged resource in your code? If so, do you write any finalizer to release the resource?

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.