Skip to content

Suspect memory leak related to RichTextBox on .Net 10.0.102 #14218

@carltonshen-work

Description

@carltonshen-work

.NET version

10.0.102

Did it work in .NET Framework?

Not tested/verified

Did it work in any of the earlier releases of .NET Core or .NET 5+?

Same code has no issue with .Net 8.0.

Issue description

I have a simple WinForm application which redirects console input/output and uses a RichTextBox for colored output.

When the program is compiled with .Net 10, it will accumulate about 10MB of memory usage every hour. Below is the GC Dump I've captured:

net10.202601161100.zip

And below is the PerfView top items for the Heap Stack view:

Name Exc % Exc Exc Ct Inc % Inc Inc Ct Fold Fold Ct
LIB <<System.Private.CoreLib!List<Runtime.InteropServices.ComWrappers+ManagedObjectWrapperHolder>>> 97.7 33,555,208 14 97.7 33,555,208 14 33,554,984 7
LIB <<System.Private.CoreLib!Diagnostics.Tracing.NativeRuntimeEventSource>> 0.6 199,730 2,457 0.6 199,730 2,457 198,886 2,443
LIB <<System.Diagnostics.Process!Diagnostics.Process>> 0.3 113,488 74 0.3 113,488 74 109,136 14
LIB <<System.Diagnostics.DiagnosticSource!Diagnostics.Metrics.MetricsEventSource>> 0.2 61,242 537 0.2 61,242 537 58,846 499
[local vars] 0.2 60,104 139 0.3 91,345 637 60,104 138
(more lines ignored)

However, if it is compiled with .Net 8.0, its memory usage is stable over time.

Steps to reproduce

Below is the procedure that is being repetitively called to add text to the RTB:

    private void AppendLineToConsoleWithColor(string text, Color color)
    {
        if (consoleOutput.InvokeRequired)
        {
            consoleOutput.Invoke(AppendLineToConsoleWithColor, text, color);
            return;
        }

        consoleOutput.SelectionStart = consoleOutput.TextLength;
        consoleOutput.SelectionLength = 0;
        consoleOutput.SelectionColor = color;

        consoleOutput.AppendText(text);
        consoleOutput.AppendText(Environment.NewLine);
        currentConsoleCharCount += text.Length;
        currentConsoleCharCount += Environment.NewLine.Length;

        consoleOutput.SelectionColor = consoleOutput.ForeColor;

        if (currentConsoleCharCount > loadedConfig.Settings.MaxConsoleChars)
        {
            int removeUpTo = consoleOutput.Text.IndexOf('\n', loadedConfig.Settings.TrimChunkSize);
            if (removeUpTo > 0)
            {
                Win32WindowAPI.SuspendRedraw(consoleOutput.Handle);
                try
                {
                    consoleOutput.Select(0, removeUpTo + 1);
                    consoleOutput.ReadOnly = false;
                    consoleOutput.SelectedText = string.Empty;
                    consoleOutput.ReadOnly = true;
                    consoleOutput.ClearUndo();
                }
                finally
                {
                    Win32WindowAPI.ResumeRedraw(consoleOutput.Handle);
                }
                currentConsoleCharCount = consoleOutput.TextLength;
            }
        }
    }

I believe anything that add text to RTB would trigger the same issue. I can already confirm the Win32 API call on the SuspendRedraw and ResumeRedraw is irrelevant, that remove them does not make any difference.

I have a test app that generates text continuously so the above code is called frequently, which I believe anything to the same effect will work.

Metadata

Metadata

Assignees

Labels

needs-area-labeluntriagedThe team needs to look at this issue in the next triage

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions