OutOfMemoryException issue
Published Sep 12 2019 07:56 AM 7,104 Views
Microsoft

If your web application showing OutOfMemoryException error, the first thing to check for more details is the Event Viewer logs. You should see which function is throwing this exception in the stack trace of the error. In the case I worked on, a variable in StringBuilder type was the root cause of this exception.

 

Solution

Checki the Private Memory Limit first. Make sure there is no limit specified or the limit entered is high enough for the web application to work properly. In order to check this feature, go to “Advanced Settings” of your application pool:

 

2.jpg

 

If IIS settings look good, it’s time to look into your code. There are three main causes of OutOfMemoryException related to the usage of StringBuilder (Reference)

  • MaxCapacity for the StringBuilder is reached
    The application is trying to expand a StringBuilder object beyond the value defined in StringBuilder.MaxCapacity property.
  • The application keeps concatenating large strings
    Continuous string concatenation may result into a large number of memory allocations, memory fragmentation, poor performance, and OutOfMemoryException exceptions
  • The application is trying to assign a large set of data to a string
    When data structures or data sets in memory become so large, CLR may not be able to allocate enough contiguous memory for them. In this case, an OutOfMemoryException exception may occur

 

If the root cause is still not clear, use DebugDiag to collect a dump. You can use DebugDiag to collect crash log, performance log, or memory leak log. In this case, collect crash log since the application is crashing with “out of memory” error.

 

Note: It’s not common but DebugDiag itself may cause performance issue in the server. I recommended running it for a specific period time and deactivating the rule once you collect dump files.

Version history
Last update:
‎Nov 09 2023 11:09 AM
Updated by: