zoukankan      html  css  js  c++  java
  • 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个字节的,我没有列出)

    第一部分暂时到此,写的大脑缺氧,过两天再写。。。
  • 相关阅读:
    Apache 虚拟主机 VirtualHost 配置
    EAX、ECX、EDX、EBX寄存器的作用
    Python中文文档 目录(转载)
    八度
    POJ 3268 Silver Cow Party (最短路)
    POJ 2253 Frogger (求每条路径中最大值的最小值,Dijkstra变形)
    2013金山西山居创意游戏程序挑战赛——复赛(1) HDU 4557 非诚勿扰 HDU 4558 剑侠情缘 HDU 4559 涂色游戏 HDU 4560 我是歌手
    HDU 4549 M斐波那契数列(矩阵快速幂+欧拉定理)
    UVA 11624 Fire! (简单图论基础)
    HDU 3534 Tree (树形DP)
  • 原文地址:https://www.cnblogs.com/juqiang/p/1023291.html
Copyright © 2011-2022 走看看