Home
%3CLINGO-SUB%20id%3D%22lingo-sub-852213%22%20slang%3D%22en-US%22%3EOutOfMemoryException%20issue%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-852213%22%20slang%3D%22en-US%22%3E%3CP%3EIf%20your%20web%20application%20showing%20OutOfMemoryException%20error%2C%20the%20first%20thing%20to%20check%20for%20more%20details%20is%20the%20Event%20Viewer%20logs.%20You%20should%20see%20which%20function%20is%20throwing%20this%20exception%20in%20the%20stack%20trace%20of%20the%20error.%20In%20the%20case%20I%20worked%20on%2C%20a%20variable%20in%20StringBuilder%20type%20was%20the%20root%20cause%20of%20this%20exception.%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-1624521790%22%20id%3D%22toc-hId-1624521790%22%3E%26nbsp%3B%3C%2FH3%3E%0A%3CH3%20id%3D%22toc-hId--927635171%22%20id%3D%22toc-hId--927635171%22%3ESolution%3C%2FH3%3E%0A%3CP%3EChecki%20the%20Private%20Memory%20Limit%20first.%20Make%20sure%20there%20is%20no%20limit%20specified%20or%20the%20limit%20entered%20is%20high%20enough%20for%20the%20web%20application%20to%20work%20properly.%20In%20order%20to%20check%20this%20feature%2C%20go%20to%20%E2%80%9C%3CSTRONG%3EAdvanced%20Settings%3C%2FSTRONG%3E%E2%80%9D%20of%20your%20application%20pool%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20638px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F131497iBD385D38C74C7A8B%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%222.jpg%22%20title%3D%222.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20IIS%20settings%20look%20good%2C%20it%E2%80%99s%20time%20to%20look%20into%20your%20code.%20There%20are%20three%20main%20causes%20of%20OutOfMemoryException%20related%20to%20the%20usage%20of%20StringBuilder%20(%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fsystem.outofmemoryexception%3Fview%3Dnetframework-4.8%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EReference%3C%2FA%3E)%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%20style%3D%22font-weight%3A%20400%3B%22%3E%3CSTRONG%3EMaxCapacity%3C%2FSTRONG%3E%3CSTRONG%3Efor%20the%20%3C%2FSTRONG%3E%3CSTRONG%3EStringBuilder%3C%2FSTRONG%3E%3CSTRONG%3Eis%20reached%3C%2FSTRONG%3E%3CBR%20%2F%3EThe%20application%20is%20trying%20to%20expand%20a%20StringBuilder%20object%20beyond%20the%20value%20defined%20in%20StringBuilder.MaxCapacity%20property.%3C%2FLI%3E%0A%3CLI%20style%3D%22font-weight%3A%20400%3B%22%3E%3CSTRONG%3EThe%20application%20keeps%20concatenating%20large%20strings%3C%2FSTRONG%3E%3CBR%20%2F%3EContinuous%20string%20concatenation%20may%20result%20into%20a%20large%20number%20of%20memory%20allocations%2C%20memory%20fragmentation%2C%20poor%20performance%2C%20and%20OutOfMemoryException%20exceptions%3C%2FLI%3E%0A%3CLI%20style%3D%22font-weight%3A%20400%3B%22%3E%3CSTRONG%3EThe%20application%20is%20trying%20to%20assign%20a%20large%20set%20of%20data%20to%20a%20string%3C%2FSTRONG%3E%3CSTRONG%3E%3CBR%20%2F%3E%3C%2FSTRONG%3EWhen%20data%20structures%20or%20data%20sets%20in%20memory%20become%20so%20large%2C%20CLR%20may%20not%20be%20able%20to%20allocate%20enough%20contiguous%20memory%20for%20them.%20In%20this%20case%2C%20an%20OutOfMemoryException%20exception%20may%20occur%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20the%20root%20cause%20is%20still%20not%20clear%2C%20use%20DebugDiag%20to%20collect%20a%20dump.%20You%20can%20use%20DebugDiag%20to%20collect%20crash%20log%2C%20performance%20log%2C%20or%20memory%20leak%20log.%20In%20this%20case%2C%20collect%20crash%20log%20since%20the%20application%20is%20crashing%20with%20%E2%80%9Cout%20of%20memory%E2%80%9D%20error.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ENote%3A%20%3C%2FSTRONG%3EIt%E2%80%99s%20not%20common%20but%20DebugDiag%20itself%20may%20cause%20performance%20issue%20in%20the%20server.%20I%20recommended%20running%20it%20for%20a%20specific%20period%20time%20and%20deactivating%20the%20rule%20once%20you%20collect%20dump%20files.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-852213%22%20slang%3D%22en-US%22%3E%3CP%20style%3D%22margin%3A%200cm%200cm%207.5pt%200cm%3B%22%3E%3CSPAN%20style%3D%22font-size%3A%2013.5pt%3B%20font-family%3A%20%26amp%3Bquot%3B%20color%3A%20%236b6b6b%3B%22%3EIf%20your%20web%20application%20showing%20OutOfMemoryException%20error%2C%20the%20first%20thing%20to%20check%20for%20more%20details%20is%20the%20Event%20Viewer%20logs.%20You%20should%20see%20which%20function%20is%20throwing%20this%20exception%20in%20the%20stack%20trace%20of%20the%20error.%20In%20the%20case%20I%20worked%20on%2C%20a%20variable%20in%20%3C%2FSPAN%3E%3CCODE%3E%3CSPAN%20style%3D%22font-size%3A%2012.0pt%3B%20font-family%3A%20Consolas%3B%20color%3A%20%23c7254e%3B%20background%3A%20%23F9F2F4%3B%22%3EStringBuilder%3C%2FSPAN%3E%3C%2FCODE%3E%3CSPAN%20style%3D%22font-size%3A%2013.5pt%3B%20font-family%3A%20%26amp%3Bquot%3B%20color%3A%20%236b6b6b%3B%22%3E%20type%20was%20the%20root%20cause%20of%20this%20exception.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E
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.