Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
539 views
in Technique[技术] by (71.8m points)

c# - Are interned strings excepted from garbage collection in .NET?

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 number of scanned objects should reduce GC time. I was wondering whether intern pool is excepted from garbage collection. There isn't anything to collect there anyway. If so, I could intern all these strings and speed up GC.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...