Basic Windbg - 1. SOSBasics(总结)

我们都知道,对于字符串相加,建议使用StringBuilder,而不是普通的string concat,为什么呢?我们通过dump简单看一下。
先看这个代码:
Code

对于hello字符串,我们加10000次,然后得到时间差。对于world字符串,我们也加10000次,然后得到时间差。按照前面讲的抓dump的方法,我们得到dump,然后看!dumpheap -stat的结果:
0:000> !dumpheap -stat
total 3686 objects
Statistics:
      MT    Count    TotalSize Class Name
791334a8        1           12 System.Collections.Generic.GenericEqualityComparer`1[[System.String, mscorlib]]
79119954        1           12 System.Security.Permissions.ReflectionPermission
79119834        1           12 System.Security.Permissions.FileDialogPermission

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
79104368      266         6384 System.Collections.ArrayList
7912d8f8      299        31408 System.Object[]
004ad640       31      1060716      Free
790fd8c4     2399      2423452 System.String
Total 3686 objects

然后我们看字符串的东西:!dumpheap -mt 790fd8c4 -strings(strings这个参数的意思是,只输出字符串,其他的都省略掉),结果如下:
0:000> !dumpheap -mt 790fd8c4 -strings
total 2399 objects
Statistics:
   Count    TotalSize String Value
       1           20 "}"
       1           20 "{"
       1           20 "x"

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
       2         5600 "<PermissionSet class="System.Security.NamedPermissionSet"versio"
       1         9280 "<NamedPermissionSets><PermissionSet class="System.Security.Name"
      10       262092 "worldworldworldworldworldworldworldworldworldworldworldworldwor"
      20      1998520 "hellohellohellohellohellohellohellohellohellohellohellohellohel"
Total 2399 objects


值得我们注意的是,world字符串(一大堆),一共有10个,大小是262092,hello字符串一共有20个,大小是1998520。两者为啥相差这么大?书上说,string concat会不停的产生临时对象,这些对象都会占用内存的,so,会比较慢,也比较吃内存。而StringBuilder是预先分配好了一块内存来用,so,会快一些。从上面的结果来看,貌似是这样的。
好,那我们修改一下代码吧!
Code
我们对StringBuilder的参数做点手脚,让它一次性的初始化64K大小,重新抓dump,然后重新用!dumpheap -mt和!dumpheap -stat来看。

       1       131096 "worldworldworldworldworldworldworldworldworldworldworldworldwor"
      20      1998520 "hellohellohellohellohellohellohellohellohellohellohellohellohel"
Total 2387 objects

这个结果比较有意思吧!如果我们让stringBuilder的buffer足够大,那么它可以避免频繁分配内存。这个例子来看,字符串只分配了两次而已(上面还有一个5个字节的,我没有列出)

第一部分暂时到此,写的大脑缺氧,过两天再写。。。
posted @ 2008-01-02 15:36  鞠强  阅读(3265)  评论(15编辑  收藏  举报

hello

world