성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
[정성태] 저렇게 조각 코드 말고, 실제로 재현이 되는 예제 프로젝트를 압...
[정성태] Modules 창(Ctrl+Shift+U)을 띄워서, 해당 Op...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>windbg로 살펴보는 GC heap의 Segment 구조</h1> <p> 지난번 GC 설명 글에서,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > .NET Garbage Collection에 대한 정리 ; <a target='tab' href='http://www.sysnet.pe.kr/2/0/1862'>http://www.sysnet.pe.kr/2/0/1862</a> </pre> <br /> 링크한 다음의 글에는 Segment에 대한 이야기가 나옵니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > How does the GC work and what are the sizes of the different generations? ; https://blogs.msdn.microsoft.com/tess/2008/04/17/how-does-the-gc-work-and-what-are-the-sizes-of-the-different-generations/ ; <a target='tab' href='https://www.tessferrandez.com/blog/2008/04/17/how-does-the-gc-work.html'>https://www.tessferrandez.com/blog/2008/04/17/how-does-the-gc-work.html</a> </pre> <br /> 이 내용을 한번 정리해 보겠습니다. ^^<br /> <br /> 우선, 위의 글에서 예로 든 Server GC를 사용하는 응용 프로그램의 경우 총 192MB 크기의 힙이 점유되어 있는데, (2 CPU를 가진 시스템의) Server GC이므로 다음과 같은 구성으로 잡혀 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Heap 0: Small Object용 segment 64MB Large Object용 segment 32MB Heap 1: Small Object용 segment 64MB Large Object용 segment 32MB </pre> <br /> 당연히 Gen0, Gen1, Gen2객체는 64MB로 할당되는 small object용 segment에서 시작합니다.<br /> <br /> 이것을 확인해 볼까요? ^^ 간단하게 코드를 만들어,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // .NET 4.7 + 32bit using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { Console.WriteLine("Press ENTER key to exit..."); Console.ReadLine(); } } } </pre> <br /> 실행 후 windbg로 연결해 힙을 보면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:007> <span style='color: blue; font-weight: bold'>!eeheap -gc</span> Number of GC Heaps: 1 <span style='color: blue; font-weight: bold'>generation 0 starts at 0x04bf1018 generation 1 starts at 0x04bf100c generation 2 starts at 0x04bf1000</span> ephemeral segment allocation context: none segment begin allocated size 04bf0000 04bf1000 04bf5ff4 0x4ff4(20468) Large object heap starts at 0x05bf1000 segment begin allocated size 05bf0000 05bf1000 05bf5500 0x4500(17664) Total Size: Size: 0x94f4 (38132) bytes. ------------------------------ GC Heap Size: Size: 0x94f4 (38132) bytes. </pre> <br /> 콘솔 프로그램이기 때문에 <a target='tab' href='http://www.sysnet.pe.kr/2/0/11445'>Workstation GC가 사용되어 GC Heap의 수는 1</a>입니다. 그리고 "ephemeral segment", 즉 small object heap으로 1개의 segment가 할당되어 있고,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > segment begin allocated size 04bf0000 04bf1000 04bf5ff4 0x4ff4(20468) </pre> <br /> Large object heap으로 또 1개의 segment가 할당되어 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > segment begin allocated size 05bf0000 05bf1000 05bf5500 0x4500(17664) </pre> <br /> SOH(Small Object Heap)의 경우 segment 시작 주소는 04bf0000이지만 begin은 04bf1000로 나옵니다. 개인적인 의견으로 앞의 0x1000 바이트는 예약된 영역이 아닌가 생각됩니다. 그런 다음 할당이 04bf5ff4로 나오는데 04bf5ff4 - 04bf0000 = 0x5ff4로 겨우 24,564 바이트에 불과합니다. 아니... 전에는 64MB가 할당된다고 하면서 왜 24,564바이트일까요? 그 이유는, <a target='tab' href='http://www.sysnet.pe.kr/2/0/1850'>64MB는 "예약(reserved)"영역이고 0x5ff4 바이트 공간은 commit 영역</a>이기 때문입니다.<br /> <br /> 여기서 아쉬운 것은 "<a target='tab' href='https://blogs.msdn.microsoft.com/tess/2008/04/17/how-does-the-gc-work-and-what-are-the-sizes-of-the-different-generations/'>How does the GC work and what are the sizes of the different generations?</a>" 글에서 소개할 당시의 sos 확장에서는 예약 영역의 크기까지도 출력이 되는 반면 현재 버전의 sos 확장에서는 그 부분이 누락되었습니다. 그렇긴 해도 해당 글에서 설명한 !address 명령어를 이용해 예약 크기를 살펴보는 것이 가능합니다.<br /> <br /> 예를 들어, SOH segment의 시작 주소 04bf0000를 !address로 살펴보면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:007> <span style='color: blue; font-weight: bold'>!address 04bf0000</span> Mapping file section regions... Mapping module regions... Mapping PEB regions... Mapping TEB and stack regions... Mapping heap regions... Mapping page heap regions... Mapping other regions... Mapping stack trace database regions... Mapping activation context regions... Usage: <unknown> <span style='color: blue; font-weight: bold'>Base Address: 04bf0000 End Address: 04c02000</span> Region Size: 00012000 ( 72.000 kB) <span style='color: blue; font-weight: bold'>State: 00001000 MEM_COMMIT</span> Protect: 00000004 PAGE_READWRITE Type: 00020000 MEM_PRIVATE Allocation Base: 04bf0000 Allocation Protect: 00000004 PAGE_READWRITE Content source: 1 (target), length: 12000 </pre> <br /> 현재 commit된 페이지가 04bf0000 ~ 04c02000까지로 0x12000(73,728, 약 72KB)만큼의 크기가 됩니다. 이후 공간을 살펴볼까요?<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:007> <span style='color: blue; font-weight: bold'>!address 04c02000</span> Usage: <unknown> <span style='color: blue; font-weight: bold'>Base Address: 04c02000 End Address: 05bf0000 Region Size: 00fee000 ( 15.930 MB) State: 00002000 MEM_RESERVE</span> Protect: <info not present at the target> Type: 00020000 MEM_PRIVATE Allocation Base: 04bf0000 Allocation Protect: 00000004 PAGE_READWRITE </pre> <br /> 보는 바와 같이 05bf0000 주소까지 15.930MB만큼 예약(reserved)되어 있습니다. 그러니까, SOH의 연속 공간으로 총 16MB의 가상 주소가 점유된 것입니다. 이것을 그림으로 정리하면 다음과 같이 됩니다.<br /> <br /> <img width='500px' alt='gc_segment_1.png' src='/SysWebRes/bbs/gc_segment_1.png' /><br /> <br /> 그런데 역시 이번에도 64MB가 아닙니다. 왜냐하면, "<a target='tab' href='https://blogs.msdn.microsoft.com/tess/2008/04/17/how-does-the-gc-work-and-what-are-the-sizes-of-the-different-generations/'>How does the GC work and what are the sizes of the different generations?</a>" 글에서 언급한 것처럼,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > How much it will allocate is depends on what framework version you use (including service packs or hotfixes), if you are running on x64 or x86 and what GC flavor the application is using (workstation or server) </pre> <br /> 환경에 따라 다르기 때문입니다. 즉, x86 + .NET 4.7의 Workstation GC에서는 SOH의 한 Segment 크기가 16MB입니다. 실제로 이 프로그램에서 Server GC 유형으로 설정해 실행하면 SOH의 한 Segment 크기가 32MB로 바뀌고 논리 CPU 만큼 생성됩니다. 예를 들어, 8개의 논리 CPU를 가진 시스템에서 x86 + .NET 4.7 + Server GC로 수행하면 32MB SOH 힙이 8개가 생성되어 총 256MB가 할당됩니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> 이어서 LOH도 유사하게 살펴볼 수 있는데요. 가만 보면, SOH의 예약 공간 주소의 끝 주소가 05bf0000로 되어 있고 LOH의 시작 주소가 05bf0000인 걸로 봐서 CLR은 초기 응용 프로그램 시작에서 SOH 예약 공간 다음에 이어서 LOH 공간을 예약하는 것으로 보입니다.<br /> <br /> !address 명령어를 통해 살펴보면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:007> <span style='color: blue; font-weight: bold'>!address 05bf0000</span> Usage: <unknown> Base Address: 05bf0000 End Address: 05c02000 <span style='color: blue; font-weight: bold'>Region Size: 00012000 ( 72.000 kB)</span> State: 00001000 MEM_COMMIT Protect: 00000004 PAGE_READWRITE Type: 00020000 MEM_PRIVATE Allocation Base: 04bf0000 Allocation Protect: 00000004 PAGE_READWRITE Content source: 1 (target), length: 12000 0:007> <span style='color: blue; font-weight: bold'>!address 05c02000</span> Usage: <unknown> Base Address: 05c02000 End Address: 06bf0000 <span style='color: blue; font-weight: bold'>Region Size: 00fee000 ( 15.930 MB)</span> State: 00002000 MEM_RESERVE Protect: <info not present at the target> Type: 00020000 MEM_PRIVATE Allocation Base: 04bf0000 Allocation Protect: 00000004 PAGE_READWRITE </pre> <br /> 최초 commit은 72KB이고 총 예약 크기는 16MB임을 알 수 있습니다. 동일한 프로그램을 Server GC로 보면 LOH의 commit 크기가 8KB이고 예약은 16MB인 것을 확인할 수 있습니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> 다시 !eeheap 명령어 결과로 가서,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:007> <span style='color: blue; font-weight: bold'>!eeheap -gc</span> Number of GC Heaps: 1 <span style='color: blue; font-weight: bold'>generation 0 starts at 0x04bf1018 generation 1 starts at 0x04bf100c generation 2 starts at 0x04bf1000</span> ephemeral segment allocation context: none segment begin allocated size 04bf0000 04bf1000 04bf5ff4 0x4ff4(20468) Large object heap starts at 0x05bf1000 segment begin allocated size 05bf0000 05bf1000 05bf5500 0x4500(17664) Total Size: Size: 0x94f4 (38132) bytes. ------------------------------ GC Heap Size: Size: 0x94f4 (38132) bytes. </pre> <br /> 이번에는 Gen 0, 1, 2 각각의 시작 주소가 다음과 같습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Gen 0 0x04bf1018 Gen 1 0x04bf100c Gen 2 0x04bf1000 </pre> <br /> 그림으로 정리해 보면 다음과 같습니다.<br /> <br /> <img width='500px' alt='gc_segment_3.png' src='/SysWebRes/bbs/gc_segment_3.png' /><br /> <br /> 이 정도면 이제 CLR GC Heap의 구조가 머릿속에 그려질 것입니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> 그렇다면, SOH 힙의 구조를 알았으니 이제 힙에 어떤 객체들이 할당되어 있는지 열람할 수 있습니다. 시작은 Gen2 주소부터 하면, 이렇게 나옵니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:007> <span style='color: blue; font-weight: bold'>!do 0x04bf1000</span> Free Object Size: 12(0xc) bytes </pre> <br /> "Free Object"라고 나오는데, 아직 한 번도 2세대 GC가 구동된 적이 없는 상태이기 때문에 reserved 의미로 할당된 듯 합니다. 이후 0x04bf100c 주소부터는 Gen 1세대 객체들인데요. <br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:007> <span style='color: blue; font-weight: bold'>!do 0x04bf100c</span> Free Object Size: 12(0xc) bytes </pre> <br /> 역시 1세대 GC도 구동된 적이 없기 때문에 reserved 의미로 1개의 빈 객체가 할당된 것 같습니다. 0세대 객체가 위치한 0x04bf1018 주소도 첫 번째 객체는 "Free Object"로 나옵니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:007> <span style='color: blue; font-weight: bold'>!do 04bf1018</span> Free Object Size: 12(0xc) bytes </pre> <br /> 음... 아무래도 0, 1, 2 세대의 첫 번째 객체는 "Free Object"로 고정된 것 같습니다. (순전히 저의 예상입니다.)<br /> <br /> 하지만, 이후부터는 정상적으로 객체들이 나옵니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:007> <span style='color: blue; font-weight: bold'>!do 04bf1018 + c</span> Name: System.Exception MethodTable: 70eefc1c EEClass: 709d6558 <span style='color: blue; font-weight: bold'>Size: 84(0x54) bytes</span> File: C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll Fields: MT Field Offset Type VT Attr Value Name 70eefad4 400028f 4 System.String 0 instance 00000000 _className 70ee4b18 4000290 8 ...ection.MethodBase 0 instance 00000000 _exceptionMethod ...[생략]... 70eb254c 40002a1 38 ...ializationManager 0 instance 00000000 _safeSerializationManager 70eefe74 400028e 5c System.Object 0 shared static s_EDILock >> Domain:Value 02c0cfe8:NotInit << 0:007> !do 04bf1018 + c + <span style='color: blue; font-weight: bold'>54</span> Name: System.OutOfMemoryException MethodTable: 70eefd70 EEClass: 70a78e04 Size: 84(0x54) bytes File: C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll Fields: MT Field Offset Type VT Attr Value Name 70eefad4 400028f 4 System.String 0 instance 00000000 _className 70ee4b18 4000290 8 ...ection.MethodBase 0 instance 00000000 _exceptionMethod ...[생략]... 70eb254c 40002a1 38 ...ializationManager 0 instance 00000000 _safeSerializationManager 70eefe74 400028e 5c System.Object 0 shared static s_EDILock >> Domain:Value 02c0cfe8:NotInit << </pre> <br /> 이런 식으로 Segment 별로 할당된 CLR 객체를 모두 열람할 수 있습니다.<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1916
(왼쪽의 숫자를 입력해야 합니다.)