c#字符串实习

Cod*_*der 11 c# string-interning

我试图理解字符串实习,为什么在我的例子中似乎不起作用.示例的要点是显示示例1使用较少(内存较少),因为它在内存中应该只有10个字符串.但是,在下面的代码中,两个示例都使用大致相同的内存量(虚拟大小和工作集).

请告知为什么示例1没有使用更少的内存?谢谢

例1:

        IList<string> list = new List<string>(10000);

        for (int i = 0; i < 10000; i++)
        {
            for (int k = 0; k < 10; k++)
            {
                list.Add(string.Intern(k.ToString()));
            }

        }

        Console.WriteLine("intern Done");
        Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)

例2:

        IList<string> list = new List<string>(10000);

        for (int i = 0; i < 10000; i++)
        {
            for (int k = 0; k < 10; k++)
            {
                list.Add(k.ToString());
            }

        }

        Console.WriteLine("intern Done");
        Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)

Dea*_*ing 17

问题是ToString()仍将分配一个新字符串,然后实习它.如果垃圾收集器没有运行来收集那些"临时"字符串,那么内存使用量将是相同的.

此外,你的字符串的长度非常短.10,000个大多数只有一个字符长的字符串是大约20KB的内存差异,您可能不会注意到这一点.尝试使用更长的字符串(或更多的字符串)并在检查内存使用情况之前进行垃圾收集.

下面是一个例子确实表现出差异:

class Program
{
    static void Main(string[] args)
    {
        int n = 100000;

        if (args[0] == "1")
            WithIntern(n);
        else
            WithoutIntern(n);
    }

    static void WithIntern(int n)
    {
        var list = new List<string>(n);

        for (int i = 0; i < n; i++)
        {
            for (int k = 0; k < 10; k++)
            {
                list.Add(string.Intern(new string('x', k * 1000)));
            }
        }

        GC.Collect();
        Console.WriteLine("Done.");
        Console.ReadLine();
    }

    static void WithoutIntern(int n)
    {
        var list = new List<string>(n);

        for (int i = 0; i < n; i++)
        {
            for (int k = 0; k < 10; k++)
            {
                list.Add(new string('x', k * 1000));
            }
        }

        GC.Collect();
        Console.WriteLine("Done.");
        Console.ReadLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 典型的微观优化,根本没有显示它应该做什么. (3认同)
  • 我编辑了我的答案,以显示一个*确实*显示差异的例子.`WithIntern`使用大约14MB的内存(根据任务管理器).第二个在大约一秒钟之后给出一个OutOfMemoryException.除非你分配了*ten*或*数百*兆字节的字符串,否则你根本不会看到任何差异. (3认同)

Bri*_*sen 7

请记住,CLR代表您的进程管理内存,因此很难通过查看虚拟大小和工作集来确定托管内存占用量.CLR通常会以块的形式分配和释放内存.它们的大小根据实现细节而有所不同,但由于这个原因,几乎不可能根据进程的内存计数器来测量托管堆使用情况.

但是,如果您查看示例的实际内存使用情况,您会看到差异.

例1

0:005>!dumpheap -stat
...
00b6911c      137         4500 System.String
0016be60        8       480188      Free
00b684c4       14       649184 System.Object[]
Total 316 objects
0:005> !eeheap -gc
Number of GC Heaps: 1
generation 0 starts at 0x01592dcc
generation 1 starts at 0x01592dc0
generation 2 starts at 0x01591000
ephemeral segment allocation context: none
 segment    begin allocated     size
01590000 01591000  01594dd8 0x00003dd8(15832)
Large object heap starts at 0x02591000
 segment    begin allocated     size
02590000 02591000  026a49a0 0x001139a0(1128864)
Total Size  0x117778(1144696)
------------------------------
GC Heap Size  0x117778(1144696)
Run Code Online (Sandbox Code Playgroud)

例2

0:006> !dumpheap -stat
...
00b684c4       14       649184 System.Object[]
00b6911c   100137      2004500 System.String
Total 100350 objects
0:006> !eeheap -gc
Number of GC Heaps: 1
generation 0 starts at 0x0179967c
generation 1 starts at 0x01791038
generation 2 starts at 0x01591000
ephemeral segment allocation context: none
 segment    begin allocated     size
01590000 01591000  0179b688 0x0020a688(2139784)
Large object heap starts at 0x02591000
 segment    begin allocated     size
02590000 02591000  026a49a0 0x001139a0(1128864)
Total Size  0x31e028(3268648)
------------------------------
GC Heap Size  0x31e028(3268648)
Run Code Online (Sandbox Code Playgroud)

从上面的输出中可以看出,第二个示例确实在托管堆上使用了更多内存.


Cor*_*ius 2

来自msdn 其次,要实习字符串,必须首先创建该字符串。String 对象使用的内存仍必须分配,即使该内存最终将被垃圾回收。