If after you upgrade your Windows or .NET Framework 4.7.1, and you notice a significant decrease in performance when you run .NET Framework applications that use the System.Diagnostics.StackFrame class, then this post might interest you. We will look at the cause, and then offer known fixes for the error.
System.Diagnostics.StackFrame performance degrade
An application that had acceptable performance running on the .NET Framework 4.7 or earlier versions executes more slowly when running on the .NET Framework 4.7.1. Applications typically rely on StackFrame when they throw .NET exceptions. If this occurs at a high rate (more than 10 incidents per second), applications can slow down significantly (tenfold) and run noticeably slower than before.
Cause of the System.Diagnostics.StackFrame performance degrade
The .NET Framework 4.7.1 in Windows 10 added support for detecting and parsing the Portable PDB file format to show file and line number information in stack traces. As part of this change, each function in a stack trace has its defining module checked to determine if that module uses the Portable PDB format. Due to some differences in the internal caching policy, the runtime spends far more time searching for Portable PDBs than previous .NET Framework versions spent searching for classic Windows PDBs.
This causes formatted stack traces to be produced more slowly than before.
This issue does not change the number of exceptions that are thrown. However, it does significantly decrease the ability of applications to handle those exceptions.
Applications that use IKVM library are known to be affected by this issue if they probe for assemblies. Probing for assemblies is known to cause exceptions.
Fix System.Diagnostics.StackFrame performance degrade issue
To resolve this issue, Microsoft recommends using either of the following methods.
1] Use a different constructor for StackFrame that takes a Boolean argument
This is the preferred solution.
If application developers are able to make changes to their applications, call the System.Diagnostics.StackTrace.#ctor(Boolean) constructor by using a false argument to avoid capturing source information. This avoids the section of the code in which performance is decreased.
2] Rollback or upgrade to the latest Windows version
In this method, roll back to the previous version/build or upgrade to the latest version/build of Windows 11/10 if you’re experiencing this issue and you’re not currently running the latest version of Windows.
Also uninstall .NET Framework 4.7.1 if present, from your computer and then download and install a previous version or the latest version of .NET Framework.
I hope this helps!