• Frugal Cafe
  • Posts
  • FC12 Memory leak to ruin your holidays II: string.Intern

FC12 Memory leak to ruin your holidays II: string.Intern

Ban string.Intern

In the previous post, we discussed MemoryCache would lead to memory leak and high CPU usage, if not disposed. Such memory leaks would ruin your holidays when there is a cloud service outrage. Here is yet another gateway to memory leak: string.Intern(string).

The primary problem with string.Intern(string) is that there is no way to clean its internal data structure, so strings would be piling up, leading to process running out of memory. The secondary problem is that its internal data structure is not thread-safe, so locking is needed for every operation. This would lead to heavy contention.

Here is a simple repro:

Tested with 1 million, managed heap grows to 46 mb:

The strings are held by object arrays in LOH.

If your code uses string.Intern, the danger comes when data source to it is unbounded. For example, data coming from external source could be unbounded, data processed by a logging filtering service could be unbounded.

General suggestions is avoid using string.Intern, and even string.IsInterned. Recommended alternative is StringTable class (StringTable.cs (sourceroslyn.io)), or use ConcurrentDictionary to implement your own cache with cap/cleanup.