-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Results.File and Results.Stream consume large amount of Memory, don't stream file directly anymore #45037
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
Comments
@davepermen Are you seeing this with HTTP/2? I assume you are using the browser to download the file? |
Yes, both cases the browser responds with "h2" (i assume it's short for HTTP/2). i the browser network tools, it states on net6: timing: it starts after 4.8ms. in net7, 3.2s. transfer itself is a bit above 3seconds in both. |
I can reproduce the issue, looking into it. |
OK this seems to be a bug in the BrowserLink middleware (something visual studio installs to do refreshing of HTML). Seems like there's a large allocation happening. @davepermen You can turn off it off with this feature in visual studio: Can you also confirm that this does not repro from the command line? |
OK it's not the browser link middleware but it seems like the BrowserRefresh middleware is also to blame here 😄. (sorry @vijayrkn and @jodavis). @MackinnonBuck it looks like this change might be the problem. We're using a memory stream to buffer the entire response body instead of the passthrough we had before. |
I can confirm, "dotnet run" from the commandline works as expected, and disabling CSS Hot Reload solves the issue in Visual Studio for now, till a proper fix is back 👍 |
Yep, we changed the BrowserRefresh middleware to buffer the entire response to improve the reliability of hot reload script injection. We could fix the memory usage problem by checking earlier whether we're working with an HTML response so we can switch back to a passthrough in cases where we know script injection can't be performed. Possible servicing candidate? cc @mkArtakMSFT. Note that this issue only affects development scenarios with hot reload enabled. |
@mkArtakMSFT @MackinnonBuck yes we need to service this. |
I think we should revert the fix and work on a more reliable solution in .NET 8. We need to get rid of the buffering. Developers doing performance testing while running visual studio are going to see huge problems with that. |
@davidfowl What is the right way to handle large files? I have so far come to the solution that I open the stream from the disk and write the data into HttpResponce, but still for a 250mb file it is a problem. I also tried to do when I receive a file from a user, first write it to disk, and then take action. I also have a http stream in memory anyway. Do you have any examples? |
The sample here is fine. The problem is our middleware that is buffering the response (as described in the replies above). |
I have spend multiple hours trying to figure out why this simple code wasnt working just create a new asp net web core web app mvc, put this in the home controller, .net 7.0.1 and vs 17.4.3
it always load everything in memory before the browser start downloading I tried disabling the CSS Hot Reload, it doesnt work it only work when i do a dotnet run |
Is there an existing issue for this?
Describe the bug
using MapGet("/", () => Results.File("bigfile.bin")) causes huge amount of memory consumption, and multiple seconds of delay till the download starts.
In .net6, download started instantly and used up no memory.
Same Issue on Results.Stream
Bug with screenshots for comparison visible here:
Expected Behavior
Results.File and Results.Stream should directly stream the file download without loading it first into ram.
Steps To Reproduce
I created a repo here, needs a big file in wwwroot/1gb.bin
https://github.com/davepermen/ResultsFileBug
to create file quickly (on windows): open cmd in wwwroot and run "fsutil file createnew 1gb.bin 1073741824" but any file works.
switch project between net6 and net7 to see the effect.
Exceptions (if any)
No response
.NET Version
7.0.100
Anything else?
No response
The text was updated successfully, but these errors were encountered: