-
Notifications
You must be signed in to change notification settings - Fork 152
Description
Simple Injector's internally creates a CyclicDependencyValidator instance per registration. Each CyclicDependencyValidator uses its own ThreadLocal<bool> instance, but this causes severe memory pressure (leak) when uses in combination with a large amount of registrations or with a large amount of container instances.
Related:
- Similar work has been done in the decorator sub system. See Remove the use of ThreadLocal<T> in the decorator sub system #540
- Very high latency for GC when using (lots of) ThreadLocal dotnet/runtime#2382
A ThreadLocal<T> causes a memory leak in case its not disposed of properly. Although the amount of memory isn't that big, it will count up in case many ThreadLocal<T> instances are created, as is the case with the CyclicDependencyValidator. Each InstanceProducer has its own CyclicDependencyValidator intance, and although the InstanceProducer removes the reference to its CyclicDependencyValidator when its no longer of use, the underlying ThreadLocal<bool> is not disposed of.
This causes memory issues as reported by @andreminelli here:
We took a memory dump and analyze it. We have spotted very large arrays (4 Million in length, in some cases) of
ThreadLocal<bool>.LinkedSlotVolatileon LOH, the majority of them "attached" toCyclicDependencyValidator. This sample had about 6GB, and LOH allocated spaced (which was very fragmented) was about 2GB.