-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Memory leak in RazorView and ViewBuffer bundle #38550
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
@sergei66666 thanks for contacting us. I don't think that For us to consider something a memory leak there needs to be an out of memory exception that can be triggered or clear evidence that the objects are being rooted after a full Gen 2 GC collection. Otherwise, its simply the behavior of the GC not running while it doesn't need to. I'm not confident that the fix proposed is correct, since I believe we manage the buffers outside of those types. Finally, I would suggest that the plan to render code outside of the HttpContext is something we track separately here, so we would recommend you upvote that issue instead. Note that we plan to do this based on Components and not MVC since they are much more lightweight |
Great job digging in to this. While we think it's an interesting problem, given that it doesn't affect the default code path, we don't think we would consider changing the runtime for this. |
Thanks for the quick answers. I want to say that I disagree with you. I will try to explain why. @javiercn, I disagree with you on the issue of memory leak. If the duration of the Scope execution is long enough, Ok, let's assume you don't support I think this is happening because the buffer is requesting memory from the pool, but it does not know when to free it. And, perhaps, if all references to all created buffers were kept, then they could be cleared when calling Namely therefore, I insist that this is a leak. Yes, it disappears when the Scope is destroyed, but inside the Scope the memory is leaked. @pranavkm Yes, now it does not affect the default code path .. But if it doesn’t affect it now, it doesn’t mean that it will not affect it in the future. Considering how difficult work with buffers inside By the way, if I start the server with the "patched" libraries, then everything goes back to the pool. ps I never claim that my fix is correct. Perhaps you see it differently. I just want to show that in the current implementation, memory is not returned to the pool at the last stage. |
This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes. See our Issue Management Policies for more information. |
Describe the bug
Hello, dear developers.
I am using the standard
RazorViewToStringRenderer
implementation to create emails.Faced a memory leak when calling
RenderViewToStringAsync
on the same instance ofRazorViewToStringRenderer
many times. For example, I call 500,000 times and it leaks approximately 200 MB of memory (leakage rate depends on the size of the layout).I get the
RazorViewToStringRenderer
instance by creating Scope. After the scope is freed, the memory is released gracefully.I understand that this is not a standard usage - on the server side there is only one rendering at a time and then the Scope is destroyed.
But maybe it's worth fixing this leak?
I did a little research, localized the problem area, made changes, and then ran my code - there is no more memory leak.
Tests for Microsoft.AspNetCore.Mvc.Razor.Test and Microsoft.AspNetCore.Mvc.ViewFeatures.Test worked correctly.
My changes are located here - sergei66666@c5712c5
Do you think it is worth making a pull request or is it not a critical issue in your opinion?
To Reproduce
Here is small project to reproduce problem: https://github.com/sergei66666/net_mvc_razor_mem_leak/
Without fix in console it will show (all values in MB)
If apply fix (in Libs folder there are two dll with changes, uncomment References in Mvc.RenderViewToString.csproj) it will show
Further technical details
dotnet --info
:dotnet --info Output
The text was updated successfully, but these errors were encountered: