Search code examples
c#stringstring-interning

Is it possible to set capacity for C# string intern pool?


Normally, when init a collection such as dictionary, if we know how many key-value pairs ahead, we could use

var dict = new Dictionary<>(capacity);

Is it possible to set capacity for C# string intern pool? (I think internally it is like a dict.)

Edits:

I am profiling a unity game.

And there are about 20 MB strings. The number of these strings are about 400000.

After intern these strings, the memory is about 7MB and the number is about 98000 strings. So I guess I could reduce memory use intern.

The reason I use intern instead of dictionary-like data structures, because the architecture issues. There is no easy way to inject such data structures.

And the strings are all config data which means they will be persistent once starts.


Solution

  • No, it is not possible to restrict the size of the intern pool.