Skip to content
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

Extremely large memory consumption in "ToListAsync()" when using cancellation token #30691

Closed
Stocki916 opened this issue Apr 14, 2023 · 6 comments

Comments

@Stocki916
Copy link

Stocki916 commented Apr 14, 2023

Dear community,

i faced a memory issue and a big difference in selecting data from a microsoft sql database when using an async "ToListAsync()" with and without a cancellation token.
Im selecting "ids" of type "bigint / long" from a table in the database by using this statement which returns 140 mio ids:

return await _context.Items.AsNoTracking().Select(s => s.Id.GetValueOrDefault(0)).Distinct().ToListAsync();

The memory consumption of the app is round about 2 GB of memory.

The largest object in memory monitor is the list with "Ids". That's clear so far :-)

If i modify the statement and pass a cancellation token to the "ToListAsync(ct)", then the memory consumption increases to > 15 GB

return await _context.Items.AsNoTracking().Select(s => s.Id.GetValueOrDefault(0)).Distinct().ToListAsync(ct);

Now there are really large "CancellationTokenSource" and "CallBacknode" objects, which cause the high memory usage:

InkedRavenDataDistribution_WithCancellationToken_LI

In general i could just remove the cancellation token but i would like to understand how i can still use a cancellation token and prevent such a large memory consumption.

@ErikEJ
Copy link
Contributor

ErikEJ commented Apr 14, 2023

Try to update to Microsoft.Data.SqlClient 5.0.2 or 5.1.1

@Stocki916
Copy link
Author

Wow that's strange! Im also using "Microsoft.Data.SqlClient" in version 5.1.1 but not directly included in the app, but using another nuget package which contains this.
So i added "Microsoft.Data.SqlClient" also directly to the app, and now the memory usage is also at 2GB WITH cancellation Token!

Thanks for your help - now i need to understand what happened here :-D

@ErikEJ
Copy link
Contributor

ErikEJ commented Apr 14, 2023

@roji Any plans to adopt 5.0.2 for EF Core 7.0.x ?

@roji
Copy link
Member

roji commented Apr 14, 2023

@ErikEJ yeah, I think we'll be doing that - though it's probably wise to wait a little bit to see that 5.0.2 is stable...

@roji
Copy link
Member

roji commented Apr 14, 2023

Duplicate of dotnet/SqlClient#1810

@roji roji marked this as a duplicate of dotnet/SqlClient#1810 Apr 14, 2023
@ErikEJ
Copy link
Contributor

ErikEJ commented Apr 14, 2023

@roji agree, issue is that it will have small adoption now that higher versions are out (5.1.x)

@roji roji changed the title Extrem large memory consumption in "ToListAsync()" when using cancellation token Extremely large memory consumption in "ToListAsync()" when using cancellation token Apr 14, 2023
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale May 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants