Are interned strings excepted from garbage collection in .NET?

前端 未结 2 1944
孤城傲影
孤城傲影 2020-12-21 01:33

I am trying to reduce time it takes to do a Gen2 collection. My app creates and holds a large number of string objects, which persist through its life.

Reducing num

2条回答
  •  不知归路
    2020-12-21 02:10

    If you are trying to reduce the total amount of memory your application allocates, keep in mind that interning a string has two unwanted side effects. First, the memory allocated for interned String objects is not likely be released until the common language runtime (CLR) terminates. The reason is that the CLR's reference to the interned String object can persist after your application, or even your application domain, terminates. Second, to intern a string, you must first create the string. The memory used by the String object must still be allocated, even though the memory will eventually be garbage collected.

    Ref: https://msdn.microsoft.com/en-us/library/system.string.intern(v=vs.110).aspx

    Emphasis mine.

提交回复
热议问题