Memory leak within WCF?

By | Jul 27, 2018

Memory leak within WCF?

So yesterday my team had to trace a possible memory issue within our application. We used the built in memory tools within Visual Studio and identified that the ‘leak’ was within our WCF infrastructure. What was strange was that the problem came from the System.ServiceModel.Channels.BufferManager which is native .net code… So how could that leak memory?

The “Leak”.

We traced the problem down to a WCF service that had to move large files around and as a result it had all the taps open.

The leak turned out not to be a “leak” but a misconfiguration of the MaxBufferPoolSize setting that resulted in undesired behavior.

MaxBufferPoolSize = 2147483647,
MaxReceivedMessageSize = 2147483647,
MaxDepth = 2147483647,
MaxStringContentLength = 2147483647,
MaxArrayLength = 2147483647,
MaxBytesPerRead = 2147483647,
MaxNameTableCharCount = 2147483647

What is the MaxBufferPoolSize

From what I understood from the limited resources on the net, this setting represents the max size of the pool of buffers when receiving data in WCF.

So when a service in WCF is configured to use buffered transport it will break the data being transmitted into buffer packets according to the size of the MaxBufferSize. Every time data is transmitted \ received a new buffer is created and added to the buffer pool. The buffer manager will only start to “manage” the memory in the pool once the pool reaches it’s limit – this is the MaxBufferPoolSize. The buffer manager will then start to GC Collect the memory and so free it… In this example the pool size is 2 GB which means that in essence 2GB of mem has been allocated to the service to use for transport. When the number is smaller it will end up using more CPU than memory so this is a balancing act.

If the MaxBufferPoolSize is set to 0 it will not be managed at all and memory will be released after receiving \ transmitting. This can be costly when it comes to CPU.


The solution is to configure the service according to your needs. By default the MaxBufferPoolSize is set to 512 K which should be good enough for most people. if you expect the service to handle more volume then you need to adjust the value accordingly. It comes down to a balance between CPU and Memory usage.

0 0 vote
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments