Are interned strings excepted from garbage collection in .NET?

前端 未结 2 1931
孤城傲影
孤城傲影 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:05

    I made a quick test and interning of strings does not seem to save them from scanning by GC. At least not in .NET 4.5 64 bit.

    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 20000000; ++i)
            {
                string s = i.ToString("X");
                string.Intern(s);
            }
            GC.Collect(3, GCCollectionMode.Forced, true);
            long t1 = Stopwatch.GetTimestamp();
            GC.Collect(3, GCCollectionMode.Forced, true);
            long t2 = Stopwatch.GetTimestamp();
            Console.WriteLine((double)(t2 - t1) / Stopwatch.Frequency);
        }
    }
    

    This benchmark returns 0.23s on an i5 3570k. If the strings are put into an array instead of interning, it returns 0.26s. If the strings are interned and created via (i % 10).ToString(), i.e. there's a small number of different instances, the benchmark returns microseconds.

    So sadly this is not a way to bypass garbage collection. I think C# should have some way of marking strings as persistent and stop the runtime wasting time on scanning them.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题