dotnet / dotnet/roslyn

[Feedback] High CPU and Memory Usage in ServiceHub.RoslynCodeAnalysisService.exe

Open
#79,925 20 comments 1 reaction 1 assignee Claimed by @RikkiGibson View on GitHub
Area-Compilers Developer Community Feature - Nullable Reference Types Priority:1
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

[Link to AzDO feedback work item](https://devdiv.visualstudio.com/DevDiv/_workitems/edit/2420692).
[Link to Developer Community feedback item](https://developercommunity.visualstudio.com/t/High-CPU-and-Memory-Usage-in-ServiceHub/10874321).
[severity:It bothers me. A fix would be nice]
I found an old [Wired article](https://www.wired.com/2008/08/four-regular-expressions-to-check-email-addresses/) with 4 regular expressions to validate email addresses. The 4th example is an expression with almost 6500 characters from a Perl module: [RegEx](https://pdw.ex-parrot.com/Mail-RFC822-Address.html)
Just for the fun of it, I tried to generate code with the C# source generator. This works surprisingly well, 30000 lines are generated in about 1 second.

Unfortunately, the CPU usage of ServiceHub.RoslynCodeAnalysisService.exe stays at about 12% and memory usage grows very fast. After 30 seconds the task used 700MB, after 60 seconds it was 3000MB and after 90 seconds 5000MB. However, Visual studio was still working without freezing or crashing.

The problem is easy to reproduce. Use this .csproj file:


Exe
net9.0
disable
enable

And the following [Program.cs](https://aka.ms/dc/file?name=B2d624d7043ad4e28b2b663044e9c9cc7638779410169473344_Program.cs&tid=2d624d7043ad4e28b2b663044e9c9cc7638779410169473344). The RegEx is in the attachment or in the given link. The other expressions from the Wired link did not cause any problems, even tough one of it has also 1000 characters.

using System;
using System.Text.RegularExpressions;

namespace RegExTest
{
public static partial class Program
{
public static void Main()
{
bool isEmail = GetIsEmailAddressRegex().IsMatch([Redacted]@[Redacted]);

Console.Write(isEmail);
}

[GeneratedRegex("", RegexOptions.Compiled)]
private static partial Regex GetIsEmailAddressRegex();
}
}
I could reproduce the problem on my current Surface Laptop with Windows 11 and ARM processor and also on my old laptop with Windows 10 and amd64 processor. I have Visual Studio 2022 v17.13.4 installed on both devices.

Note that numerous issues were reported about ServiceHub.RoslynCodeAnalysisService.exe and high CPU/memory usage: [This](https://developercommunity.visualstudio.com/t/High-memory-usage-of-ServiceHubRoslynCo/1668250), [This](https://developercommunity.visualstudio.com/t/ServiceHubRoslynCodeAnalysisServiceexe/1428683) and [This](https://developercommunity.visualstudio.com/t/High-CPU-Usage-in-ServiceHubRoslynCodeA/10641615). I obviously still have the problem and have provided an minimal example to reproduce the issue.

Have fun with analyzing the RegEx 😃

To find diagnostic information, see the [original linked feedback ticket](https://devdiv.visualstudio.com/DevDiv/_workitems/edit/2420692).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.