1024programmer Asp.Net Net Advanced Debugging 11: Managed Heap Layout Architecture and Object Allocation Mechanism

Net Advanced Debugging 11: Managed Heap Layout Architecture and Object Allocation Mechanism

Net Advanced Debugging 11: Managed Heap Layout Architecture and Object Allocation Mechanism

1. Introduction
Today is the eleventh article of “Net Advanced Debugging”. This article comes a bit late. , because I have been very busy recently and have no time to write articles. Now we finally have some time to continue our series. In this article, we mainly introduce the architecture of the managed heap, the object allocation mechanism, and how we find objects on the managed heap. After studying this chapter, many concepts that were vague before are now very clear, and I know the allocation of object generations. , the structure of the large object heap and the small object heap, and understanding the life cycle of the objects. These are the bottom layers of the Net framework. A deeper understanding will be more beneficial to us in debugging. Of course, when you watch a video or read a book for the first time, you will be very confused and don’t know how to operate it. As the old saying goes, if it doesn’t work once, then try it again. If it doesn’t work, then try it again. As the saying goes, you can read a thousand books. All over, its meaning appears by itself.
If there is no explanation, the test environment for all codes is Net Framewok 4.8. However, sometimes in order to view the source code, you may need to use Net Core projects. I will explain it in the project chapter. Okay, without further ado, let’s start our debugging work today.

 I need to explain the debugging environment in case everyone is unclear. I have listed the specific circumstances.
     Operating system: Windows Professional 10
   Debugging tool: Windbg Preview (you can go to Microsoft Store to download)
 Development Tool: Visual Studio 2022
   Net version: Net Framework 4.8
   CoreCLR source code: Source code download

2. Basic knowledge
  
 1. Managed heap and garbage collection
   1. Windows Memory architecture
 To understand the memory allocation mechanism of C#, you first need to understand the memory allocation mechanism of Windows. After all, the memory in the CLR is allocated from Windows. The architecture diagram is as follows:
=”font-size: 16px”>2. CLR Heap Manager

  2.1. Introduction
      CLR Heap Manager Hosting The memory is divided into two large blocks.
     a. Divided by object size
                                                                                                                                                                                                                                            banks you in in in ining Objects > 85000 byte can be stored in the large object heap ( LOH)].

   b. Divided by survival period.
                                                                                     ccogene Up in in in>>\ a new object is often more likely to become a garbage object, so the efficiency of recycling these objects will be higher, so a [generation mechanism] has been made in [Small Object Heap (SOH)] Division, that is, generation 0, generation 1, and generation 2.

   2.2. Managed heap layout diagram
                                                                                                                                                                                                                                                                              < ), the newly generated segment is called a temporary segment, and the others are called segment Longer segment. The small object heap has the concept of temporary segments, but the large object heap does not. The structure is as shown in the figure:
, in the small object heap, the latest created is the temporary segment, the others are memory segments, and so on. All 0-generation objects and 1-generation objects will be allocated in the temporary segment. Part of the 2-generation objects will be placed in the temporary segment. Other segments, such as the memory segment, will always store 2-generation objects.

    2.3. Workstation and server GC
      CLR has two GC modes, namely: workstation GC and server GC.
a), workstation GC
Workstation GC generally refers to applications with window classes, such as: WinForm, WPF, SliverLight, Console. Such programs have only one managed heap.
                                                                                    handling you in in in on the effect :
    (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 span> 015c4e80 10 Free
20 0337100c 015c4e80 10 Free
21 03371018 span> 015c4e80 10 Free
22 . . . . . . .

We look at the objects in the large object heap.

 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  span> 03371000 03377ff4 0x6ff4(28660)
  9 Large object heap starts at 0x04371000
 10  segment begin allocated size
 11 04370000  span> 04371000 0438a180 0x19180(102784)
 12 Total Size: Size: 0x20174 (131444) bytes.
 13 -----------------------  --------
 14 GC Heap Size: Size: 0x20174   (131444) bytes  .
 15 0  span>:006> !dumpheap 04371000   0438a180
 16  Address MT Size
 17 04371000  span> 015c4e80 10 Free
 18 04371010  span> 015c4e80 14 Free
 19 04371020  span> 6da92788 4872
 20 04372328 015c4e80 14 Free
 21 04372338  span> 6da92788 524
 22 04372548 015c4e80 14 Free
 23 04372558  span> 6da92788 4092
 24 04373558 015c4e80 14 Free
 25 04373568  span> 6da92788 8172
 26 04375558 015c4e80 14 Free
 27 04375568  span> 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\

Four. Summary
 Finally finished writing. Since I am quite busy this week, The speed of writing articles has also slowed down. I have no choice but to do my job well first. Work is my duty. As the old saying goes, although I am very busy and the writing process is quite tiring, I am still very happy when I see my growth. The learning process is really not that easy. Fortunately, I like this industry better, otherwise I don’t know if I can persist. Let’s talk about the old saying again. The first time I read the book “Advanced Debugging”, I was really confused. The second time it was a little better. If you don’t learn, you won’t know. Once you learn, you will be shocked. You will find that you lack a lot. Okay, let’s not talk anymore. Don’t forget your original intention and continue to work hard. I hope God will not let down those who work hard.

All countries in the world can be equalized; honors and positions can be dismissed; swordsmanship can be practiced; moderation is impossible.”>0438a180
16 Address MT Size
17 04371000 span> 015c4e80 10 Free
18 04371010 span> 015c4e80 14 Free
19 04371020 span> 6da92788 4872
20 04372328 015c4e80 14 Free
21 04372338 span> 6da92788 524
22 04372548 015c4e80 14 Free
23 04372558 span> 6da92788 4092
24 04373558 015c4e80 14 Free
25 04373568 span> 6da92788 8172
26 04375558 015c4e80 14 Free
27 04375568 span> 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\

Four. Summary
 Finally finished writing. Since I am quite busy this week, The speed of writing articles has also slowed down. I have no choice but to do my job well first. Work is my duty. As the old saying goes, although I am very busy and the writing process is quite tiring, I am still very happy when I see my growth. The learning process is really not that easy. Fortunately, I like this industry better, otherwise I don’t know if I can persist. Let’s talk about the old saying again. The first time I read the book “Advanced Debugging”, I was really confused. The second time it was a little better. If you don’t learn, you won’t know. Once you learn, you will be shocked. You will find that you lack a lot. Okay, let’s not talk anymore. Don’t forget your original intention and continue to work hard. I hope God will not let down those who work hard.

All countries in the world can be equalized; honors and positions can be dismissed; swordsmanship can be practiced; moderation is impossible.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/811164

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索