I have a WCF project (C #) and I ran into OutOfMemoryException problems. DLLs are built using Any CPU . AppPool memory settings are set to 0 (which means there are no restrictions). I have about 1,500 requests per minute constantly and about 100 other requests per minute. The project uses EntityFramework. The application has caching (this is a dictionary)
I tried to find out the cause of the problem, but so far it is unclear. I tried to calculate the shared memory used (by calling GC.GetTotalMemory(false) ) and the size of the cache list. When OOM exceptions are received, the cache size is about 7 MB (2500 objects of 30 KB each), and the total used memory varies from 600 MB to 1.5 GB.
So, it is obvious that the memory is not full and resources are regularly cleaned using GarbageCollector (the memory size does not increase all the time). Thus, the OOM exception does not occur because the memory is full.
Basically, I see OOM exceptions in the logs when serializing objects in JSON (I use Newtonsoft) or applying some string operations (concatenation, replacement, Regexreplace, ...), here are some examples of exceptions:
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. at System.Text.StringBuilder.ExpandByABlock(Int32 minBlockCharCount) at System.Text.StringBuilder.Append(Char* value, Int32 valueCount) at System.Text.StringBuilder.Append(String value, Int32 startIndex, Int32 count) at System.Text.RegularExpressions.RegexReplacement.Replace(Regex regex, String input, Int32 count, Int32 startat) at System.Text.RegularExpressions.Regex.Replace(String input, String replacement, Int32 count, Int32 startat) at System.Text.RegularExpressions.Regex.Replace(String input, String replacement) at System.Text.RegularExpressions.Regex.Replace(String input, String pattern, String replacement)
and
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. at System.String.ReplaceInternal(String oldValue, String newValue) at System.String.Replace(String oldValue, String newValue)
Any ideas or suggestions on what can be done to reproduce the problem?