Skip to content

Memory leak in RazorView and ViewBuffer bundle #38550

Closed
@sergei66666

Description

@sergei66666

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 of RazorViewToStringRenderer 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)

Mem0: 127,16015625
Mem1: 312,8046875

If apply fix (in Libs folder there are two dll with changes, uncomment References in Mvc.RenderViewToString.csproj) it will show

Mem0: 126,984375
Mem1: 113,6640625

Further technical details

  • ASP.NET Core version: 6.0.0
  • The IDE: Microsoft Visual Studio Community 2022 (64-bit) Version 17.0.1
  • Include the output of dotnet --info:
dotnet --info Output
Пакет SDK для .NET (отражающий любой global.json):
 Version:   6.0.100
 Commit:    9e8b04bbff

Среда выполнения:
 OS Name:     Windows
 OS Version:  10.0.19043
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.100\

Host (useful for support):
  Version: 6.0.0
  Commit:  4822e3c3aa

.NET SDKs installed:
  2.1.4 [C:\Program Files\dotnet\sdk]
  2.1.801 [C:\Program Files\dotnet\sdk]
  2.2.401 [C:\Program Files\dotnet\sdk]
  5.0.403 [C:\Program Files\dotnet\sdk]
  6.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Status: Resolvedarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-mvc-razor-viewsFeatures related to the Razor view engine for Razor pages and MVC views

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions