Autofac, Owin and memory leaks. Is autofac leaking

2019-07-15 01:50发布

Recently I realized there is a leak in my Web API application based on Owin and Autofac. The leak is not permanent but memory kept longer than per request. Here's the deal:

1-) By default Web API uses buffered responses and the runtime keeps OverlappedData structure up to 40 instances as an object pool even after the response is returned. (For performance reasons I guess)

2-) Each OverlappedData structure keeps the OwinContext alive (only 40 at a time probably for pooling)

3-) Because OwinContext is alive, the associated Autofac container per request scope is kept alive though the container itself is disposed.

4-) Even if Autofac container is disposed, it is keeping the objects in its sharedInstances dictionary.

Hence the memory leak. A workaround is to use streaming instead of buffering and then there is no OverlappedData. But I am curious about another solution.

Another weird thing is if an Autofac container is disposed it is still keeping the data in its dictionary. It should set the dictionary to null instead! Any ideas ?

0条回答
登录 后发表回答