今天是《Net 高級(jí)調(diào)試》的第十一篇文章,這篇文章來(lái)的有點(diǎn)晚,因?yàn)椋罱容^忙,就沒(méi)時(shí)間寫文章了?,F(xiàn)在終于有點(diǎn)時(shí)間,繼續(xù)開始我們這個(gè)系列。這篇文章我們主要介紹托管堆的架構(gòu),對(duì)象的分配機(jī)制,我們?nèi)绾尾檎以谕泄芏焉系膶?duì)象,我學(xué)完這章,很多以前很模糊的概念,現(xiàn)在很清晰了,知道了對(duì)象代的分配,大對(duì)象堆和小對(duì)象堆的結(jié)構(gòu),了解了對(duì)象的生命周期,這些是 Net 框架的底層,了解更深,對(duì)于我們調(diào)試更有利。當(dāng)然了,第一次看視頻或者看書,是很迷糊的,不知道如何操作,還是那句老話,一遍不行,那就再來(lái)一遍,還不行,那就再來(lái)一遍,俗話說(shuō)的好,書讀千遍,其意自現(xiàn)。
如果在沒(méi)有說(shuō)明的情況下,所有代碼的測(cè)試環(huán)境都是 Net Framewok 4.8,但是,有時(shí)候?yàn)榱瞬榭丛创a,可能需要使用 Net Core 的項(xiàng)目,我會(huì)在項(xiàng)目章節(jié)里進(jìn)行說(shuō)明。好了,廢話不多說(shuō),開始我們今天的調(diào)試工作。
?? 調(diào)試環(huán)境我需要進(jìn)行說(shuō)明,以防大家不清楚,具體情況我已經(jīng)羅列出來(lái)。
操作系統(tǒng):Windows Professional 10
調(diào)試工具:Windbg Preview(可以去Microsoft Store 去下載)
開發(fā)工具:Visual Studio 2022
Net 版本:Net Framework 4.8
CoreCLR源碼:源碼下載
二、基礎(chǔ)知識(shí)
1、托管堆和垃圾回收
1、Windows 內(nèi)存架構(gòu)
要了解 C# 的內(nèi)存分配機(jī)制,首先需要了解 Windows 內(nèi)存分配的機(jī)制,畢竟 CLR 中的內(nèi)存是從 Windows 上分配過(guò)來(lái)的。架構(gòu)圖如下:

2、CLR堆管理器
2.1、簡(jiǎn)介
CLR 堆管理器托管的內(nèi)存劃分成兩大塊。
a、按對(duì)象大小劃分
所有小于 85000 byte 的對(duì)象都存放在【小對(duì)象堆(SOH)】,大于等于 85000 byte 的對(duì)象存放在【大對(duì)象堆(LOH)】。
b、按生存期劃分。
CLR 假設(shè)一個(gè)新分配的對(duì)象往往更容易成為一個(gè)垃圾對(duì)象,所以回收這些對(duì)象的效率會(huì)更高,所以在【小對(duì)象堆(SOH)】做了一個(gè)【代機(jī)制】的劃分,也就是 0代、1代、2代?! ?br>
2.2、托管堆布局圖
托管堆是由很多的段(segment)組成的,新生成的 segment 叫做臨時(shí)段,其他的叫 segment 年長(zhǎng)段。小對(duì)象堆有臨時(shí)段的概念,大對(duì)象堆是沒(méi)有的。結(jié)構(gòu)如圖:

托管堆是由段組成的,在小對(duì)象堆中,最新創(chuàng)建的是臨時(shí)段,其他則是內(nèi)存段,依次類推。所有的0代對(duì)象和1代對(duì)象都會(huì)分配在臨時(shí)段上,2代對(duì)象會(huì)有一部分放在臨時(shí)段上,其他的段,比如:內(nèi)存段,永遠(yuǎn)存放的是2代對(duì)象。
2.3、工作站和服務(wù)器GC
CLR 有兩種 GC 模式,分別是:工作站 GC 和 服務(wù)器GC。
a)、工作站GC
工作站GC 一般指具有窗口類的應(yīng)用程序,比如:WinForm,WPF,SliverLight,Console,這樣的程序只有一個(gè)托管堆。
效果如圖:

b)、服務(wù)器GC
對(duì)于 Web 類的程序,一般默認(rèn)使用 服務(wù)器GC,它的托管堆個(gè)數(shù)和當(dāng)前機(jī)器 CPU 的核數(shù)一致。
如圖:

3、對(duì)象分配
當(dāng)我們?cè)趯?duì)象堆中分配一個(gè)對(duì)象時(shí),大致流程如下:
a)、將一個(gè)對(duì)象分配到托管堆上。
b)、如果托管堆的空間不足,將會(huì)觸發(fā) GC。
c)、GC 觸發(fā)之后,如果空間足夠的話,就會(huì)存放對(duì)象。
d)、如果垃圾對(duì)象帶有析構(gòu)函數(shù),那么將會(huì)進(jìn)入到【可終結(jié)隊(duì)列】中被執(zhí)行。
過(guò)程如圖:

4、dumpheap 命令介紹
為了能夠高效的篩選托管堆中的對(duì)象,SOS 提供了一個(gè)【!dumpheap】命令,這個(gè)命令十分強(qiáng)大,可以幫助我們很方便的篩選?!?br>
三、調(diào)試過(guò)程
廢話不多說(shuō),這一節(jié)是具體的調(diào)試過(guò)程,又可以說(shuō)是眼見為實(shí)的過(guò)程,在開始之前,我還是要啰嗦兩句,這一節(jié)分為兩個(gè)部分,第一部分是測(cè)試的源碼部分,沒(méi)有代碼,當(dāng)然就談不上測(cè)試了,調(diào)試必須有載體。第二部分就是根據(jù)具體的代碼來(lái)證實(shí)我們學(xué)到的知識(shí),是具體的眼見為實(shí)。
1、調(diào)試源碼
1.1、Example_11_1_1


1 namespace Example_11_1_1 2 { 3 internal class Program 4 { 5 static void Main(string[] args) 6 { 7 byte[] byte1 = new byte[10000]; 8 byte[] byet2 = new byte[85000]; 9 Console.WriteLine("Hello world!"); 10 Console.ReadLine(); 11 } 12 } 13 }
1.2、Example_11_1_2
這個(gè)項(xiàng)目很簡(jiǎn)單,就是建立一個(gè) Asp.Net WebAPI 的項(xiàng)目,不需要寫任何代碼,使用默認(rèn)功能就可以。
1.3、Example_11_1_3


1 namespace Example_11_1_3 2 { 3 internal class Program 4 { 5 static void Main(string[] args) 6 { 7 byte[] byte1 = new byte[85000]; 8 byte[] byte2 = new byte[1500]; 9 byte[] byet3 = new byte[3500]; 10 Console.WriteLine("3 個(gè) byte[] 分配完畢!"); 11 Console.ReadLine(); 12 } 13 } 14 }
2、眼見為實(shí)
項(xiàng)目的所有操作都是一樣的,所以就在這里說(shuō)明一下,但是每個(gè)測(cè)試?yán)?,都需要重新啟?dòng),并加載相應(yīng)的應(yīng)用程序,加載方法都是一樣的。流程如下:我們編譯項(xiàng)目,打開 Windbg,點(diǎn)擊【文件】----》【launch executable】附加程序,打開調(diào)試器的界面,程序已經(jīng)處于中斷狀態(tài)。
2.1、我們查看 NT 堆和 GC 堆。
調(diào)試源碼:Example_11_1_1
1)、我們先來(lái)查看一下 NT 堆。
當(dāng)我們成功進(jìn)入調(diào)試器界面,使用【g】命令,繼續(xù)運(yùn)行,我們會(huì)在12行代碼【Console.ReadLine()】暫停,我們程序打印出了【Hello world】,我們點(diǎn)擊調(diào)試器工具欄中的【break】按鈕,就可以調(diào)試程序了。
我們使用【!address -summary 】命令,查看一下具體情況。


1 0:006> !address -summary 2 3 4 Mapping file section regions... 5 Mapping module regions... 6 Mapping PEB regions... 7 Mapping TEB and stack regions... 8 Mapping heap regions... 9 Mapping page heap regions... 10 Mapping other regions... 11 Mapping stack trace database regions... 12 Mapping activation context regions... 13 14 --- Usage Summary ---------------- RgnCount ----------- Total Size -------- %ofBusy %ofTotal 15 Free 57 f852b000 ( 3.880 GB) 97.00% 16 Image 191 2eb9000 ( 46.723 MB) 38.06% 1.14% 17 <unknown> 88 2670000 ( 38.438 MB) 31.31% 0.94% 18 MappedFile 18 1cec000 ( 28.922 MB) 23.56% 0.71% 19 Stack 21 700000 ( 7.000 MB) 5.70% 0.17% 20 Heap 10 13b000 ( 1.230 MB) 1.00% 0.03% 21 Other 7 5c000 ( 368.000 kB) 0.29% 0.01% 22 TEB 7 16000 ( 88.000 kB) 0.07% 0.00% 23 PEB 1 3000 ( 12.000 kB) 0.01% 0.00% 24 25 --- Type Summary (for busy) ------ RgnCount ----------- Total Size -------- %ofBusy %ofTotal 26 MEM_IMAGE 198 2ec3000 ( 46.762 MB) 38.09% 1.14% 27 MEM_PRIVATE 122 2ebc000 ( 46.734 MB) 38.07% 1.14% 28 MEM_MAPPED 23 1d46000 ( 29.273 MB) 23.84% 0.71% 29 30 --- State Summary ---------------- RgnCount ----------- Total Size -------- %ofBusy %ofTotal 31 MEM_FREE 57 f852b000 ( 3.880 GB) 97.00% 32 MEM_RESERVE 65 43a4000 ( 67.641 MB) 55.10% 1.65% 33 MEM_COMMIT 278 3721000 ( 55.129 MB) 44.90% 1.35% 34 35 --- Protect Summary (for commit) - RgnCount ----------- Total Size -------- %ofBusy %ofTotal 36 PAGE_EXECUTE_READ 32 25b2000 ( 37.695 MB) 30.70% 0.92% 37 PAGE_READONLY 90 bae000 ( 11.680 MB) 9.51% 0.29% 38 PAGE_WRITECOPY 30 399000 ( 3.598 MB) 2.93% 0.09% 39 PAGE_READWRITE 102 1f8000 ( 1.969 MB) 1.60% 0.05% 40 PAGE_READWRITE | PAGE_GUARD 16 28000 ( 160.000 kB) 0.13% 0.00% 41 PAGE_EXECUTE_READWRITE 8 8000 ( 32.000 kB) 0.03% 0.00% 42 43 --- Largest Region by Usage ----------- Base Address -------- Region Size ---------- 44 Free 80010000 7f2f0000 ( 1.987 GB) 45 Image 6de80000 f55000 ( 15.332 MB) 46 <unknown> 2f22000 fee000 ( 15.930 MB) 47 MappedFile 19d4000 133d000 ( 19.238 MB) 48 Stack 1410000 fd000 (1012.000 kB) 49 Heap f20000 8b000 ( 556.000 kB) 50 Other ff480000 33000 ( 204.000 kB) 51 TEB ce1000 4000 ( 16.000 kB) 52 PEB ccc000 3000 ( 12.000 kB)
輸出的內(nèi)容還是不少的,列表中【Heap 10 13b000 ( 1.230 MB) 1.00% 0.03%】,這個(gè)就是 NT 堆。
我們也可以使用【!heap -s】命令,查看 NT 堆的詳情。
1 0:006> !heap -s 2 3 4 ************************************************************************************************************************ 5 NT HEAP STATS BELOW 6 ************************************************************************************************************************ 7 NtGlobalFlag enables following debugging aids for new heaps: 8 tail checking 9 free checking 10 validate parameters 11 LFH Key : 0x0afd8ea9 12 Termination on corruption : ENABLED 13 Heap Flags Reserv Commit Virt Free List UCR Virt Lock Fast 14 (k) (k) (k) (k) length blocks cont. heap 15 ----------------------------------------------------------------------------- 16 00f20000 40000062 1020 556 1020 6 65 1 0 4 (第一個(gè)堆是進(jìn)程堆,是 Win32函數(shù)用的) 17 01200000 40001062 60 12 60 1 2 1 0 0 18 01570000 40001062 60 12 60 1 2 1 0 0 19 02f00000 40001062 60 4 60 0 1 1 0 0 20 01540000 40041062 60 4 60 2 1 1 0 0 21 -----------------------------------------------------------------------------
2)、我們查看一下 GC 堆。
如果我們想查看 GC 堆,可以直接使用【!eeheap -gc】命令。
1 0:006> !eeheap -gc 2 Number of GC Heaps: 1 3 generation 0 starts at 0x02f11018 4 generation 1 starts at 0x02f1100c 5 generation 2 starts at 0x02f11000 6 ephemeral segment allocation context: none 7 segment begin allocated size 8 02f10000 02f11000 02f1871c 0x771c(30492) 9 Large object heap starts at 0x03f11000 10 segment begin allocated size 11 03f10000 03f11000 03f2a180 0x19180(102784) 12 Total Size: Size: 0x2089c (133276) bytes. 13 ------------------------------ 14 GC Heap Size: Size: 0x2089c (133276) bytes.
2.2、我們查看大對(duì)象和小對(duì)象分配機(jī)制
調(diào)試源碼:Example_11_1_1
當(dāng)我們成功進(jìn)入調(diào)試器界面,使用【g】命令,繼續(xù)運(yùn)行,我們會(huì)在12行代碼【Console.ReadLine()】暫停,我們程序打印出了【Hello world】,我們點(diǎn)擊調(diào)試器工具欄中的【break】按鈕,就可以調(diào)試程序了。
byte[] byte1 = new byte[10000];這行數(shù)組小于85000就應(yīng)該在小對(duì)象堆中,byte[] byet2 = new byte[85000];這個(gè)數(shù)組對(duì)象大于85000 就會(huì)分配在大對(duì)象堆中。
我們先使用【!eeheap -gc】查看一下托管堆的情況。
1 0:006> !eeheap -gc 2 Number of GC Heaps: 1 3 generation 0 starts at 0x02f11018 4 generation 1 starts at 0x02f1100c 5 generation 2 starts at 0x02f11000 6 ephemeral segment allocation context: none 7 segment begin allocated size 8 02f10000 02f11000 02f1871c 0x771c(30492) 9 Large object heap starts at 0x03f11000(這個(gè)就是大對(duì)象堆) 10 segment begin allocated size 11 03f10000 03f11000 03f2a180 0x19180(102784) 12 Total Size: Size: 0x2089c (133276) bytes. 13 ------------------------------ 14 GC Heap Size: Size: 0x2089c (133276) bytes.
我們?cè)偈褂谩?dumpheap 03f11000 03f2a180】查看一下大對(duì)象堆的情況。
0:006> !dumpheap 03f11000 03f2a180 Address MT Size 03f11000 00f45368 10 Free 03f11010 00f45368 14 Free 03f11020 6dae2788 4872 03f12328 00f45368 14 Free 03f12338 6dae2788 524 03f12548 00f45368 14 Free 03f12558 6dae2788 4092 03f13558 00f45368 14 Free 03f13568 6dae2788 8172 03f15558 00f45368 14 Free 03f15568 6dae5c40 85012 Statistics: MT Count TotalSize Class Name 00f45368 6 80 Free 6dae2788 4 17660 System.Object[] 6dae5c40 1 85012 System.Byte[] Total 11 objects
紅色標(biāo)注的就是我們的 byte 數(shù)組,大小是 85012,為什么多了12,數(shù)組是引用類型,引用類型都有兩個(gè)附加字段(8)和一個(gè)數(shù)組長(zhǎng)度(4)的字段,共12,再加上數(shù)組的長(zhǎng)度就是85012。
我們確認(rèn)了 byet2 確實(shí)是在大對(duì)象堆中,我們繼續(xù)看看 byet1是不是在小對(duì)象堆中。使用相同的命令【!dumpheap 02f11000 02f1871c】,這個(gè)地址就是小對(duì)象堆的起始地址和結(jié)束地址,就是【ephemeral segment allocation】臨時(shí)段。
1 0:006> !dumpheap 02f11000 02f1871c 2 Address MT Size 3 02f11000 00f45368 10 Free 4 02f1100c 00f45368 10 Free 5 ..... 6 02f124fc 00f45368 10 Free 7 02f12508 6dae5c40 10012 8 02f14c24 6dae8b20 28 9 ..... 10 02f174f4 6db49b0c 16 11 12 Statistics: 13 MT Count TotalSize Class Name 14 ...... 15 6dae5c40 4 10818 System.Byte[] 16 Total 325 objects
紅色標(biāo)記的就是 byte 數(shù)組,我們使用【!DumpHeap /d -mt 6dae5c40】查看詳情。
1 0:006> !DumpHeap /d -mt 6dae5c40 2 Address MT Size 3 02f12508 6dae5c40 10012 4 02f16710 6dae5c40 526 5 02f171cc 6dae5c40 268 6 02f174e8 6dae5c40 12 7 03f15568 6dae5c40 85012 8 9 Statistics: 10 MT Count TotalSize Class Name 11 6dae5c40 5 95830 System.Byte[] 12 Total 5 objects
紅色標(biāo)記的就是我們的 byte1 byte 數(shù)組。當(dāng)然,我們可以使用命令【!do】查看詳情。
1 0:006> !do 02f12508 2 Name: System.Byte[] 3 MethodTable: 6dae5c40 4 EEClass: 6dbe8ba8 5 Size: 10012(0x271c) bytes 6 Array: Rank 1, Number of elements 10000, Type Byte (Print Array) 7 Content: ................................................................................................................................ 8 Fields: 9 None
2.3、如何按生存期查看對(duì)象的分配。
調(diào)試源碼:Example_11_1_1
當(dāng)我們成功進(jìn)入調(diào)試器界面,使用【g】命令,繼續(xù)運(yùn)行,我們會(huì)在12行代碼【Console.ReadLine()】暫停,我們程序打印出了【Hello world】,我們點(diǎn)擊調(diào)試器工具欄中的【break】按鈕,就可以調(diào)試程序了。
其實(shí),有關(guān)對(duì)象代的調(diào)試,我們已經(jīng)做過(guò)了,這里正式測(cè)試一下,我們依然使用【!eeheap -gc】命令,就可以看到托管堆中的代了。
1 0:006> !eeheap -gc 2 Number of GC Heaps: 1 3 generation 0 starts at 0x02f11018(0代) 4 generation 1 starts at 0x02f1100c(1代) 5 generation 2 starts at 0x02f11000(2代) 6 ephemeral segment allocation context: none 7 segment begin allocated size 8 02f10000 02f11000 02f1871c 0x771c(30492) 9 Large object heap starts at 0x03f11000 10 segment begin allocated size 11 03f10000 03f11000 03f2a180 0x19180(102784) 12 Total Size: Size: 0x2089c (133276) bytes. 13 ------------------------------ 14 GC Heap Size: Size: 0x2089c (133276) bytes.
2.4、查看 Console GC 模式
調(diào)試源碼:Example_11_1_1
當(dāng)我們成功進(jìn)入調(diào)試器界面,使用【g】命令,繼續(xù)運(yùn)行,我們會(huì)在12行代碼【Console.ReadLine()】暫停,我們程序打印出了【Hello world】,我們點(diǎn)擊調(diào)試器工具欄中的【break】按鈕,就可以調(diào)試程序了。
我們可以使用【!eeversion】命令,查看GC模式。
1 0:006> !eeversion 2 4.8.4300.0 retail 3 Workstation mode(工作站模式) 4 SOS Version: 4.8.4300.0 retail build
我們也可以通過(guò)【!eeheap -gc】命令查看托管堆的個(gè)數(shù)查看。
1 0:006> !eeheap -gc 2 Number of GC Heaps: 1(只有一個(gè)托管堆,就是工作站模式) 3 generation 0 starts at 0x02f11018 4 generation 1 starts at 0x02f1100c 5 generation 2 starts at 0x02f11000 6 ephemeral segment allocation context: none 7 segment begin allocated size 8 02f10000 02f11000 02f1871c 0x771c(30492) 9 Large object heap starts at 0x03f11000 10 segment begin allocated size 11 03f10000 03f11000 03f2a180 0x19180(102784) 12 Total Size: Size: 0x2089c (133276) bytes. 13 ------------------------------ 14 GC Heap Size: Size: 0x2089c (133276) bytes.
2.5、查看 Asp.Net Web API 的 GC 模式
調(diào)試源碼:Example_11_1_2
這里測(cè)試的源碼時(shí) Web API 項(xiàng)目,直接運(yùn)行程序,然后我們通過(guò) Windbg 的【attach to Process】命令來(lái)查看。附加進(jìn)程的進(jìn)程是【iisexpress】,效果如圖:

如果你使用的是【Debug】運(yùn)行的 WEBAPI,調(diào)試會(huì)失敗,附加進(jìn)程有誤,如圖:
在 Windbg中提示的具體錯(cuò)誤:"The process that you are attempting to attach to is already being debugged. Only one debugger can be invasively attached to a process at a time. A non-invasive attach is still possible when another debugger is attached." ,意思就是:嘗試附加到的進(jìn)程已在調(diào)試中。一次只能將一個(gè)調(diào)試器侵入性附加到進(jìn)程。把程序的運(yùn)行模式改為【Release】模式,不用使用調(diào)試模式,快捷鍵:Ctrl+F5,就可以附加進(jìn)程成功了。
我們可以使用【!eeheap -gc】命令查看一下服務(wù)器GC模式。
1 0:037> !eeheap -gc 2 Number of GC Heaps: 4(有四個(gè)堆,這既是服務(wù)器GC模式) 3 ------------------------------ 4 Heap 0 (000001ff28a2dc70) 5 generation 0 starts at 0x000001ff29283e08 6 generation 1 starts at 0x000001ff29121018 7 generation 2 starts at 0x000001ff29121000 8 ephemeral segment allocation context: none 9 segment begin allocated size 10 000001ff29120000 000001ff29121000 000001ff2a79a728 0x1679728(23566120) 11 Large object heap starts at 0x0000020329121000 12 segment begin allocated size 13 0000020329120000 0000020329121000 000002032941f2c0 0x2fe2c0(3138240) 14 Heap Size: Size: 0x19779e8 (26704360) bytes. 15 ------------------------------ 16 Heap 1 (000001ff28a5dc40) 17 generation 0 starts at 0x000002002923b6d0 18 generation 1 starts at 0x0000020029121018 19 generation 2 starts at 0x0000020029121000 20 ephemeral segment allocation context: none 21 segment begin allocated size 22 0000020029120000 0000020029121000 000002002a755fe8 0x1634fe8(23285736) 23 Large object heap starts at 0x0000020339121000 24 segment begin allocated size 25 0000020339120000 0000020339121000 00000203392c8ff0 0x1a7ff0(1736688) 26 Heap Size: Size: 0x17dcfd8 (25022424) bytes. 27 ------------------------------ 28 Heap 2 (000001ff28a87bf0) 29 generation 0 starts at 0x00000201291ebe00 30 generation 1 starts at 0x0000020129121018 31 generation 2 starts at 0x0000020129121000 32 ephemeral segment allocation context: none 33 segment begin allocated size 34 0000020129120000 0000020129121000 000002012993ffe8 0x81efe8(8515560) 35 Large object heap starts at 0x0000020349121000 36 segment begin allocated size 37 0000020349120000 0000020349121000 0000020349121018 0x18(24) 38 Heap Size: Size: 0x81f000 (8515584) bytes. 39 ------------------------------ 40 Heap 3 (000001ff28ab1ba0) 41 generation 0 starts at 0x00000202291ebab8 42 generation 1 starts at 0x0000020229121018 43 generation 2 starts at 0x0000020229121000 44 ephemeral segment allocation context: none 45 segment begin allocated size 46 0000020229120000 0000020229121000 000002022a711fe8 0x15f0fe8(23007208) 47 Large object heap starts at 0x0000020359121000 48 segment begin allocated size 49 0000020359120000 0000020359121000 00000203592d9ec8 0x1b8ec8(1806024) 50 Heap Size: Size: 0x17a9eb0 (24813232) bytes. 51 ------------------------------ 52 GC Heap Size: Size: 0x511d870 (85055600) bytes.
當(dāng)然,我們也可以使用【!eeversion】查看 GC 的模式。
1 0:037> !eeversion 2 4.8.4300.0 free 3 Server mode with 4 gc heaps(服務(wù)器模式) 4 SOS Version: 4.8.4300.0 retail build
我的機(jī)器當(dāng)前又4個(gè)CPU,截圖如下:
所以,服務(wù)器 GC 模式就有 4個(gè) 托管堆。
2.6、【!dumpheap】命令使用簡(jiǎn)介調(diào)試源碼:Example_11_1_3
當(dāng)我們成功進(jìn)入調(diào)試器界面,使用【g】命令,繼續(xù)運(yùn)行,我們會(huì)在12行代碼【Console.ReadLine()】暫停,我們程序打印出了【3 個(gè) byte[] 分配完畢!】,我們點(diǎn)擊調(diào)試器工具欄中的【break】按鈕,就可以調(diào)試程序了,開始我們的篩選調(diào)試。
如果我們直接使用【!dumpheap】命令,列出的內(nèi)容太多了,想要從中查找一個(gè)對(duì)象,好像大海撈針一樣,所以我們就需要過(guò)濾。
a)、我們過(guò)濾對(duì)象大小在1000-2000之間的對(duì)象。
我們執(zhí)行命令【!dumpheap -min 0n1000 -max0n2000】,0n是十進(jìn)制,默認(rèn)0x十六進(jìn)制。
1 0:006> !dumpheap -min 0n1000 -max 0n2000 2 Address MT Size 3 03372514 6da95c40 1512 4 03375690 6da92c60 1660 5 6 Statistics: 7 MT Count TotalSize Class Name 8 6da95c40 1 1512 System.Byte[] 9 6da92c60 1 1660 System.Char[] 10 Total 2 objects
我們?cè)賮?lái)一個(gè)錯(cuò)誤的演示,把0n去掉,執(zhí)行如下;
1 0:006> !dumpheap -min 1000 -max 2000 2 Address MT Size 3 04371020 6da92788 4872 4 04373568 6da92788 8172 5 6 Statistics: 7 MT Count TotalSize Class Name 8 6da92788 2 13044 System.Object[] 9 Total 2 objects
把 0n 去掉,我們看看1000和2000是多少。
1 0:006> ? 1000 2 Evaluate expression: 4096 = 00001000 3 0:006> ? 2000 4 Evaluate expression: 8192 = 00002000
看到了把,查找的范圍就是4000多到8000多,意思就不對(duì)了,所以一定要加上 0n。
b)、我要找到大于1000的對(duì)象,我們可以使用【!dumpheap -min 0n1000】,去掉 -max 就可以了。
1 0:006> !dumpheap -min 0n1000 2 Address MT Size 3 03372514 6da95c40 1512 4 03372afc 6da95c40 3512 5 03375690 6da92c60 1660 6 04371020 6da92788 4872 7 04372558 6da92788 4092 8 04373568 6da92788 8172 9 04375568 6da95c40 85012 10 11 Statistics: 12 MT Count TotalSize Class Name 13 6da92c60 1 1660 System.Char[] 14 6da92788 3 17136 System.Object[] 15 6da95c40 3 90036 System.Byte[] 16 Total 7 objects
都是我們要查找的對(duì)象。
我們可以使用【!dumpheap -stat】命令,統(tǒng)計(jì)一下托管堆對(duì)象的結(jié)果,我們只關(guān)注 String。
1 0:006> !dumpheap -stat 2 Statistics: 3 MT Count TotalSize Class Name 4 6da95468 1 12 System.Collections.Generic.GenericEqualityComparer`1[[System.String, mscorlib]] 5 ...... 6 6da924e4 166 5926 System.String 7 6da92788 6 17748 System.Object[] 8 6da95c40 6 90842 System.Byte[] 9 Total 337 objects
紅色標(biāo)記的我們要查找的字符串。


1 0:006> !dumpheap -type string 2 Address MT Size 3 4 Statistics: 5 MT Count TotalSize Class Name 6 Total 0 objects 7 0:006> !dumpheap -type String 8 Address MT Size 9 03371228 6da924e4 14 10 03371254 6da924e4 208 11 03371324 6da924e4 258 12 03371498 6da924e4 22 13 033714b0 6da924e4 78 14 03371544 6da924e4 244 15 03371638 6da924e4 28 16 03371664 6da924e4 50 17 03371698 6da92d74 84 18 033716ec 6da924e4 208 19 033717bc 6da924e4 64 20 033717fc 6da924e4 28 21 03371818 6da924e4 16 22 03371828 6da924e4 38 23 03371850 6da924e4 44 24 0337187c 6da924e4 46 25 033718ac 6da924e4 34 26 033718d0 6da924e4 28 27 033718ec 6da924e4 30 28 0337190c 6da924e4 38 29 03371934 6da924e4 58 30 03371970 6da924e4 58 31 033719ac 6da924e4 64 32 033719ec 6da924e4 52 33 03371a20 6da924e4 50 34 03371a54 6da924e4 44 35 03371a80 6da924e4 44 36 03371aac 6da924e4 30 37 03371acc 6da924e4 42 38 03371af8 6da924e4 56 39 03371b30 6da924e4 36 40 03371b54 6da924e4 24 41 03371b6c 6da924e4 106 42 03371bd8 6da924e4 148 43 03371c6c 6da924e4 78 44 03371cbc 6da924e4 70 45 03371d04 6da924e4 82 46 03372494 6da924e4 66 47 033724d8 6da92d74 12 48 033724e4 6da924e4 46 49 033738f0 6da924e4 16 50 03373a04 6da924e4 40 51 03373aac 6da924e4 30 52 03373acc 6da924e4 26 53 03373b60 6da924e4 78 54 03373bf8 6da924e4 16 55 03373c08 6da924e4 16 56 03373c18 6da924e4 16 57 03373c28 6da924e4 16 58 03373c38 6da924e4 90 59 03373c94 6da924e4 18 60 03373ca8 6da924e4 50 61 03373cdc 6da924e4 18 62 03373cf0 6da924e4 48 63 03373d20 6da924e4 16 64 03373d30 6da924e4 16 65 03373d40 6da924e4 16 66 03373d50 6da924e4 16 67 03373d60 6da924e4 16 68 03373d70 6da924e4 16 69 03373d80 6da924e4 16 70 03373d90 6da924e4 16 71 03373da0 6da924e4 16 72 03373db0 6da924e4 16 73 03373dc0 6da924e4 16 74 03373dd0 6da924e4 20 75 03373de4 6da924e4 30 76 03373e04 6da924e4 32 77 03373e24 6da924e4 16 78 03373e34 6da924e4 16 79 03373e44 6da924e4 16 80 03373e54 6da924e4 20 81 03373e68 6da924e4 68 82 03373eac 6da924e4 18 83 03373ec0 6da924e4 18 84 03373ed4 6da924e4 30 85 03373ef4 6da924e4 24 86 03373f0c 6da924e4 30 87 03373f2c 6da924e4 22 88 03373f44 6da924e4 28 89 03373f60 6da924e4 24 90 03373f78 6da924e4 20 91 03373f8c 6da924e4 20 92 03373fa0 6da924e4 20 93 03373fb4 6da924e4 20 94 03373fd4 6da924e4 40 95 03373ffc 6da924e4 114 96 033741bc 6da92d74 52 97 03374210 6da92d74 16 98 03374220 6da92d74 28 99 0337423c 6da92d74 16 100 033742c4 6da924e4 50 101 033742f8 6da924e4 34 102 0337431c 6da924e4 34 103 03374340 6da924e4 50 104 03374374 6da924e4 32 105 03374394 6da924e4 28 106 033743b0 6da924e4 22 107 033743c8 6da924e4 18 108 033743dc 6da924e4 26 109 033743f8 6da924e4 26 110 03374414 6da924e4 28 111 03374430 6da924e4 32 112 03374450 6da924e4 30 113 03374470 6da924e4 26 114 0337448c 6da924e4 30 115 033744ac 6da924e4 20 116 033744c0 6da924e4 20 117 033744d4 6da924e4 20 118 033744e8 6da924e4 20 119 033744fc 6da924e4 20 120 03374510 6da924e4 20 121 03374524 6da924e4 20 122 03374538 6da924e4 18 123 0337454c 6da924e4 18 124 03374560 6da924e4 18 125 03374574 6da924e4 18 126 03374588 6da924e4 18 127 0337459c 6da924e4 18 128 033745b0 6da924e4 18 129 033745c4 6da924e4 28 130 033745e0 6da924e4 30 131 03374600 6da924e4 24 132 03374618 6da924e4 24 133 03374630 6da924e4 20 134 03374644 6da924e4 22 135 0337465c 6da924e4 22 136 03374674 6da924e4 26 137 03374690 6da924e4 32 138 033746b0 6da924e4 28 139 033746cc 6da924e4 30 140 033746ec 6da924e4 30 141 0337470c 6da924e4 20 142 03374720 6da924e4 20 143 03374734 6da924e4 20 144 03374748 6da924e4 20 145 0337475c 6da924e4 20 146 03374770 6da924e4 20 147 03374784 6da924e4 20 148 03374798 6da924e4 20 149 033747ac 6da924e4 20 150 033747c0 6da924e4 20 151 033747d4 6da924e4 20 152 0337483c 6da92d74 20 153 03374850 6da92d74 16 154 03374860 6da92d74 16 155 03374870 6da92d74 16 156 03374880 6da92d74 16 157 03374890 6da92d74 16 158 033748a0 6da92d74 40 159 033748c8 6da92d74 40 160 033748f0 6da92d74 40 161 03374918 6da92d74 64 162 03374958 6da92d74 64 163 03374998 6da924e4 26 164 033749b4 6da924e4 26 165 033749d0 6da924e4 24 166 03374a30 6da9524c 28 167 03374a64 6da924e4 24 168 03374a7c 6da952a0 48 169 03374ae4 6da95468 12 170 03374c24 6da924e4 24 171 03374c3c 6da924e4 24 172 03374c54 6da924e4 24 173 03374c6c 6da924e4 24 174 03374c84 6da924e4 24 175 03374c9c 6da924e4 24 176 03374cd8 6da955bc 60 177 03374d14 6da924e4 24 178 03374d88 6da924e4 26 179 03374da4 6da924e4 22 180 03374df4 6da9524c 28 181 03374fb4 6da92d74 52 182 03374fe8 6da924e4 16 183 03374ff8 6da924e4 20 184 0337500c 6da924e4 16 185 0337501c 6da924e4 16 186 0337502c 6da924e4 16 187 0337503c 6da924e4 16 188 0337504c 6da924e4 62 189 0337512c 6da924e4 30 190 0337514c 6da924e4 26 191 033755b0 6da924e4 24 192 033755c8 6da924e4 24 193 033755e0 6da924e4 32 194 03375600 6da924e4 32 195 03375620 6da924e4 32 196 03375d98 6da924e4 24 197 03375db0 6da924e4 26 198 199 Statistics: 200 MT Count TotalSize Class Name 201 6da95468 1 12 System.Collections.Generic.GenericEqualityComparer`1[[System.String, mscorlib]] 202 6da952a0 1 48 System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Globalization.CultureData, mscorlib]] 203 6da9524c 2 56 System.Text.StringBuilder 204 6da955bc 1 60 System.Collections.Generic.Dictionary`2+Entry[[System.String, mscorlib],[System.Globalization.CultureData, mscorlib]][] 205 6da92d74 18 608 System.String[] 206 6da924e4 166 5926 System.String 207 Total 189 objects
字符串對(duì)象還是挺多的,如果想看效果,可以點(diǎn)開查看。
當(dāng)然,如果我們知道字符串的方法表,也可以找到的到,這個(gè)樣更精確?!?dumpheap -type String】是模糊查找,所有和字符串相關(guān)都會(huì)找到,我們使用【!dumpheap -mt 6da924e4】,這個(gè)才最準(zhǔn)確。


1 0:006> !dumpheap -mt 6da924e4 2 Address MT Size 3 03371228 6da924e4 14 4 03371254 6da924e4 208 5 03371324 6da924e4 258 6 03371498 6da924e4 22 7 033714b0 6da924e4 78 8 03371544 6da924e4 244 9 03371638 6da924e4 28 10 03371664 6da924e4 50 11 033716ec 6da924e4 208 12 033717bc 6da924e4 64 13 033717fc 6da924e4 28 14 03371818 6da924e4 16 15 03371828 6da924e4 38 16 03371850 6da924e4 44 17 0337187c 6da924e4 46 18 033718ac 6da924e4 34 19 033718d0 6da924e4 28 20 033718ec 6da924e4 30 21 0337190c 6da924e4 38 22 03371934 6da924e4 58 23 03371970 6da924e4 58 24 033719ac 6da924e4 64 25 033719ec 6da924e4 52 26 03371a20 6da924e4 50 27 03371a54 6da924e4 44 28 03371a80 6da924e4 44 29 03371aac 6da924e4 30 30 03371acc 6da924e4 42 31 03371af8 6da924e4 56 32 03371b30 6da924e4 36 33 03371b54 6da924e4 24 34 03371b6c 6da924e4 106 35 03371bd8 6da924e4 148 36 03371c6c 6da924e4 78 37 03371cbc 6da924e4 70 38 03371d04 6da924e4 82 39 03372494 6da924e4 66 40 033724e4 6da924e4 46 41 033738f0 6da924e4 16 42 03373a04 6da924e4 40 43 03373aac 6da924e4 30 44 03373acc 6da924e4 26 45 03373b60 6da924e4 78 46 03373bf8 6da924e4 16 47 03373c08 6da924e4 16 48 03373c18 6da924e4 16 49 03373c28 6da924e4 16 50 03373c38 6da924e4 90 51 03373c94 6da924e4 18 52 03373ca8 6da924e4 50 53 03373cdc 6da924e4 18 54 03373cf0 6da924e4 48 55 03373d20 6da924e4 16 56 03373d30 6da924e4 16 57 03373d40 6da924e4 16 58 03373d50 6da924e4 16 59 03373d60 6da924e4 16 60 03373d70 6da924e4 16 61 03373d80 6da924e4 16 62 03373d90 6da924e4 16 63 03373da0 6da924e4 16 64 03373db0 6da924e4 16 65 03373dc0 6da924e4 16 66 03373dd0 6da924e4 20 67 03373de4 6da924e4 30 68 03373e04 6da924e4 32 69 03373e24 6da924e4 16 70 03373e34 6da924e4 16 71 03373e44 6da924e4 16 72 03373e54 6da924e4 20 73 03373e68 6da924e4 68 74 03373eac 6da924e4 18 75 03373ec0 6da924e4 18 76 03373ed4 6da924e4 30 77 03373ef4 6da924e4 24 78 03373f0c 6da924e4 30 79 03373f2c 6da924e4 22 80 03373f44 6da924e4 28 81 03373f60 6da924e4 24 82 03373f78 6da924e4 20 83 03373f8c 6da924e4 20 84 03373fa0 6da924e4 20 85 03373fb4 6da924e4 20 86 03373fd4 6da924e4 40 87 03373ffc 6da924e4 114 88 033742c4 6da924e4 50 89 033742f8 6da924e4 34 90 0337431c 6da924e4 34 91 03374340 6da924e4 50 92 03374374 6da924e4 32 93 03374394 6da924e4 28 94 033743b0 6da924e4 22 95 033743c8 6da924e4 18 96 033743dc 6da924e4 26 97 033743f8 6da924e4 26 98 03374414 6da924e4 28 99 03374430 6da924e4 32 100 03374450 6da924e4 30 101 03374470 6da924e4 26 102 0337448c 6da924e4 30 103 033744ac 6da924e4 20 104 033744c0 6da924e4 20 105 033744d4 6da924e4 20 106 033744e8 6da924e4 20 107 033744fc 6da924e4 20 108 03374510 6da924e4 20 109 03374524 6da924e4 20 110 03374538 6da924e4 18 111 0337454c 6da924e4 18 112 03374560 6da924e4 18 113 03374574 6da924e4 18 114 03374588 6da924e4 18 115 0337459c 6da924e4 18 116 033745b0 6da924e4 18 117 033745c4 6da924e4 28 118 033745e0 6da924e4 30 119 03374600 6da924e4 24 120 03374618 6da924e4 24 121 03374630 6da924e4 20 122 03374644 6da924e4 22 123 0337465c 6da924e4 22 124 03374674 6da924e4 26 125 03374690 6da924e4 32 126 033746b0 6da924e4 28 127 033746cc 6da924e4 30 128 033746ec 6da924e4 30 129 0337470c 6da924e4 20 130 03374720 6da924e4 20 131 03374734 6da924e4 20 132 03374748 6da924e4 20 133 0337475c 6da924e4 20 134 03374770 6da924e4 20 135 03374784 6da924e4 20 136 03374798 6da924e4 20 137 033747ac 6da924e4 20 138 033747c0 6da924e4 20 139 033747d4 6da924e4 20 140 03374998 6da924e4 26 141 033749b4 6da924e4 26 142 033749d0 6da924e4 24 143 03374a64 6da924e4 24 144 03374c24 6da924e4 24 145 03374c3c 6da924e4 24 146 03374c54 6da924e4 24 147 03374c6c 6da924e4 24 148 03374c84 6da924e4 24 149 03374c9c 6da924e4 24 150 03374d14 6da924e4 24 151 03374d88 6da924e4 26 152 03374da4 6da924e4 22 153 03374fe8 6da924e4 16 154 03374ff8 6da924e4 20 155 0337500c 6da924e4 16 156 0337501c 6da924e4 16 157 0337502c 6da924e4 16 158 0337503c 6da924e4 16 159 0337504c 6da924e4 62 160 0337512c 6da924e4 30 161 0337514c 6da924e4 26 162 033755b0 6da924e4 24 163 033755c8 6da924e4 24 164 033755e0 6da924e4 32 165 03375600 6da924e4 32 166 03375620 6da924e4 32 167 03375d98 6da924e4 24 168 03375db0 6da924e4 26 169 170 Statistics: 171 MT Count TotalSize Class Name 172 6da924e4 166 5926 System.String 173 Total 166 objects
d)、我們可以搜索大對(duì)象堆或者小對(duì)象堆中的對(duì)象,我們可以執(zhí)行【!dumpheap】命令,命令后可以跟 Segment 段的開始和結(jié)束地址。
我們查看小對(duì)象堆的對(duì)象。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-747859.html
1 0:006> !eeheap -gc 2 Number of GC Heaps: 1 3 generation 0 starts at 0x03371018 4 generation 1 starts at 0x0337100c 5 generation 2 starts at 0x03371000 6 ephemeral segment allocation context: none 7 segment begin allocated size 8 03370000 03371000 03377ff4 0x6ff4(28660) 9 Large object heap starts at 0x04371000 10 segment begin allocated size 11 04370000 04371000 0438a180 0x19180(102784) 12 Total Size: Size: 0x20174 (131444) bytes. 13 ------------------------------ 14 GC Heap Size: Size: 0x20174 (131444) bytes. 15 16 17 0:006> !dumpheap 03371000 03377ff4 18 Address MT Size 19 03371000 015c4e80 10 Free 20 0337100c 015c4e80 10 Free 21 03371018 015c4e80 10 Free 22 。。。。。。。
我們查看大對(duì)象堆的對(duì)象。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-747859.html
1 0:006> !eeheap -gc 2 Number of GC Heaps: 1 3 generation 0 starts at 0x03371018 4 generation 1 starts at 0x0337100c 5 generation 2 starts at 0x03371000 6 ephemeral segment allocation context: none 7 segment begin allocated size 8 03370000 03371000 03377ff4 0x6ff4(28660) 9 Large object heap starts at 0x04371000 10 segment begin allocated size 11 04370000 04371000 0438a180 0x19180(102784) 12 Total Size: Size: 0x20174 (131444) bytes. 13 ------------------------------ 14 GC Heap Size: Size: 0x20174 (131444) bytes. 15 0:006> !dumpheap 04371000 0438a180 16 Address MT Size 17 04371000 015c4e80 10 Free 18 04371010 015c4e80 14 Free 19 04371020 6da92788 4872 20 04372328 015c4e80 14 Free 21 04372338 6da92788 524 22 04372548 015c4e80 14 Free 23 04372558 6da92788 4092 24 04373558 015c4e80 14 Free 25 04373568 6da92788 8172 26 04375558 015c4e80 14 Free 27 04375568 6da95c40 85012 28 29 Statistics: 30 MT Count TotalSize Class Name 31 015c4e80 6 80 Free 32 6da92788 4 17660 System.Object[] 33 6da95c40 1 85012 System.Byte[] 34 Total 11 objects
2.7\
四、總結(jié)
終于寫完了,由于這周比較忙,寫文章的速度也慢了下來(lái)了,沒(méi)辦法,要先把工作做好,工作是本分。還是老話,雖然很忙,寫作過(guò)程也挺累的,但是看到了自己的成長(zhǎng),心里還是挺快樂(lè)的。學(xué)習(xí)過(guò)程真的沒(méi)那么輕松,還好是自己比較喜歡這一行,否則真不知道自己能不能堅(jiān)持下來(lái)。老話重談,《高級(jí)調(diào)試》的這本書第一遍看,真的很暈,第二遍稍微好點(diǎn),不學(xué)不知道,一學(xué)嚇一跳,自己欠缺的很多。好了,不說(shuō)了,不忘初心,繼續(xù)努力,希望老天不要辜負(fù)努力的人。
到了這里,關(guān)于Net 高級(jí)調(diào)試之十一:托管堆布局架構(gòu)和對(duì)象分配機(jī)制的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!