dotnet / SqlClient

Microsoft.Data.SqlClient provides database connectivity to SQL Server for .NET applications.
MIT License
861 stars 289 forks source link

Memory Leak SqlConnectionFactory #2406

Open Juandavi1 opened 8 months ago

Juandavi1 commented 8 months ago

Describe the bug

Hi !

We have a multi-tenant db model. Each user has a different connection string.

We have a base class that creates the connections and data accessors.

Something like this:

image


This is our invocation


image


This is our gcdump


image


This is our Memory Graph

image

Further technical details

Microsoft.Data.SqlClient version: 5.2.0 .NET target: net 6 SQL Server version: Azure sql database Level compatibility 160 Version 16 Operating system: Docker

Additional context

We've already conducted tests with pooling set to both true and false, and the problem persists.

Our connection string looks like this:


server={0};uid={1};pwd={2};database={3};connection timeout=5;pooling=true;Max Pool Size=100;Workstation ID=Microservices;Connection Lifetime=60;Application Name=ms-memory;

At night, when traffic is zero, the memory does not decrease. We also tried calling the garbage collector (GC) manually, and the memory decreased by a few MB.

javiermendozain commented 8 months ago

Hi @DavoudEshtehari and @Juandavi1

In this PR, some memory leaks have been fixed. https://github.com/dotnet/SqlClient/pull/2420

DavoudEshtehari commented 8 months ago

Thank you for proposing the PR. We'll look into it.

Juandavi1 commented 8 months ago

Hi ! We found something. We are calling SqlConnection.ClearPool() after each query and the memory leak disappear. However the performance increase a lot !

I was wondering 🤔 Is there a way to have the same pool for diferente connections? Do you know? @DavoudEshtehari

Juandavi1 commented 8 months ago

https://github.com/DapperLib/Dapper/issues/1759

Juandavi1 commented 7 months ago

If we set pooling to false, throws a too many noon-pooled connections exception