성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
글쓰기
제목
이름
암호
전자우편
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'>C# - static 필드의 정보가 GC Heap에 저장될까요?</h1> <p> 아래의 질문에서,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C# 정적 클래스 ; <a target='tab' href='https://forum.dotnetdev.kr/t/c/156'>https://forum.dotnetdev.kr/t/c/156</a> </pre> <br /> 다른 건 그냥 넘어가고, static 필드의 값이 과연 어디에 저장되느냐... 하는 것이 다소 흥미로울 수 있습니다. 사실 이에 대해서는 예전 글에서,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > windbg - 닷넷 메모리 덤프에서 정적(static) 필드 값을 조사하는 방법 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/11487'>https://www.sysnet.pe.kr/2/0/11487</a> </pre> <br /> 이미 유사하게 다뤘습니다. 그래도 ^^ 이참에 전용 주제로 한 번 다뤄보도록 하겠습니다.<br /> <br /> <hr style='width: 50%' /><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;' > using System; class Program { static void Main(string[] args) { Console.WriteLine("Press any key to continue..."); Console.ReadLine(); AccessField(); Console.WriteLine("Press any key to exit..."); Console.ReadLine(); } private unsafe static void AccessField() { Console.WriteLine(MyClass.Age); fixed (int* ptr = &(MyClass.Age)) { IntPtr addr = new IntPtr(ptr); Console.WriteLine("0x" + addr.ToInt64().ToString("x")); } } } public class MyClass { static public int Age = 5; static public string Name = "tester";} } </pre> <br /> 실행 후, 첫 번째 Console.ReadLine에 멈췄을 때 windbg로 연결합니다. 당연히 지금 시점에는 MyClass의 어떠한 인스턴스도 new를 하지 않았으므로 GC Heap에는 MyClass 타입과 관련한 정보를 찾을 수 없습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:000> <span style='color: blue; font-weight: bold'>!dumpheap -type MyClass</span> Address MT Size Statistics: MT Count TotalSize Class Name Total 0 objects </pre> <br /> 게다가 Type 정보 자체도 아직 메모리에 로드하지 않았음을 !name2ee 명령어 실행 결과로 알 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:000> <span style='color: blue; font-weight: bold'>!name2ee ConsoleApp1!MyClass</span> Module: 00007ff916485158 Assembly: ConsoleApp1.exe Token: 0000000002000003 <span style='color: blue; font-weight: bold'>MethodTable: <not loaded yet> EEClass: <not loaded yet></span> Name: MyClass </pre> <br /> 자, 그럼 계속 진행하기 위해 'g' 키를 누르고 MyClass.Age 필드를 접근하기 위해 enter 키를 한 번 누르면 화면에는 이런 식으로 출력이 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Press any key to continue... 5 0x7ff9164857ec Press any key to exit... </pre> <br /> 당연히 이 시점에는 정적 생성자도 호출이 되었고, (두 번째 Console.ReadLine 덕분에) 다시 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:008> <span style='color: blue; font-weight: bold'>!name2ee ConsoleApp1!MyClass</span> Module: 00007ff916485158 Assembly: ConsoleApp1.exe Token: 0000000002000003 <span style='color: blue; font-weight: bold'>MethodTable: 00007ff916485b20 EEClass: 00007ff916482868</span> Name: MyClass </pre> <br /> 이렇게 타입 정보가 로드된 것을 확인할 수 있습니다. 그리고 Age와 Name 필드의 값은 !dumpclass 명령어로 확인할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:008> <span style='color: blue; font-weight: bold'>!dumpclass 00007ff916482868</span> Class Name: MyClass mdToken: 0000000002000003 File: C:\temp\ConsoleApp1\ConsoleApp1\bin\Debug\ConsoleApp1.exe Parent Class: 00007ff9739d2f68 Module: 00007ff916485158 Method Table: 00007ff916485b20 Vtable Slots: 4 Total Method Slots: 6 Class Attributes: 100001 Transparency: Critical NumInstanceFields: 0 NumStaticFields: 2 MT Field <span style='color: blue; font-weight: bold'>Offset</span> Type VT Attr <span style='color: blue; font-weight: bold'>Value Name</span> 00007ff9739f85a0 4000001 <span style='color: blue; font-weight: bold'>3c</span> System.Int32 1 static <span style='color: blue; font-weight: bold'>5 Age</span> 00007ff9739f59c0 4000002 <span style='color: blue; font-weight: bold'>8</span> System.String 0 static <span style='color: blue; font-weight: bold'>00000000024a77c8 Name</span> </pre> <br /> 보는 바와 같이 Age == 5, Name == 00000000024a77c8로 Age의 경우 값 타입이므로 그 값을 그대로 들고 있으며, Name의 경우에는 참조 타입이므로 (string 데이터 자체는 GC Heap에 담겨 있지만) 데이터가 위치한 GC Heap의 주솟값을 들고 있게 됩니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> 그런데, 저걸 보고 왜? static 필드 값이 GC Heap에 보관돼 있지 않다고 말할 수 있는 걸까요?<br /> <br /> 이것은, GC Heap의 주소 영역을 덤프해 보면 알 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > windbg로 살펴보는 GC heap의 Segment 구조 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/11446'>https://www.sysnet.pe.kr/2/0/11446</a> </pre> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:008> <span style='color: blue; font-weight: bold'>!eeheap -gc</span> Number of GC Heaps: 1 generation 0 starts at 0x00000000024a1030 generation 1 starts at 0x00000000024a1018 generation 2 starts at 0x00000000024a1000 ephemeral segment allocation context: none segment begin allocated size <span style='color: blue; font-weight: bold'>00000000024a0000</span> 00000000024a1000 <span style='color: blue; font-weight: bold'>00000000024a7fe8</span> 0x6fe8(28648) Large object heap starts at 0x00000000124a1000 segment begin allocated size <span style='color: blue; font-weight: bold'>00000000124a0000</span> 00000000124a1000 <span style='color: blue; font-weight: bold'>00000000124a9a90</span> 0x8a90(35472) Total Size: Size: 0xfa78 (64120) bytes. ------------------------------ GC Heap Size: Size: 0xfa78 (64120) bytes. </pre> <br /> 보는 바와 같이 Age 필드의 주솟값은 저 GC Heap의 SOH, LOH 어느 힙에도 속해 있지 않습니다. 반면, MyClass의 Name 필드가 담고 있는 00000000024a77c8 주솟값은 SOH 세그먼트 영역(00000000024a0000 ~ 00000000024a7fe8) 안을 가리키고 있습니다. (즉, string 데이터 자체는 GC Heap에 담겨 있습니다.)<br /> <br /> 또한 이것은 (당연하겠지만) 스레드의 stack 영역도 아닙니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:000> <span style='color: blue; font-weight: bold'>? @rsp</span> Evaluate expression: 5499480 = 00000000`0053ea58 </pre> <br /> <hr style='width: 50%' /><br /> <br /> 그런데, 위의 결과에서 이상한 점이 있습니다. 저는 지금까지, static 필드의 값이 EEClass나 MethodTable 정보 영역에 보관돼 있다고 짐작하고 있었는데 이번 실습을 하면서 그 가정이 깨졌습니다.<br /> <br /> 위에서 실습한 대로, !dumpclass의 출력 결과에 보면 분명히 "Offset" 값이 있는데 Age 필드의 주솟값을 기준으로 역추적해 보면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:008> <span style='color: blue; font-weight: bold'>? 0x7ff9164857ec - 0x3c</span> Evaluate expression: 140707797424048 = <span style='color: blue; font-weight: bold'>00007ff9`164857b0</span> </pre> <br /> 나오는 이 영역이 이상합니다. 다른 출력 결과와 비교해도 분명히 MethodTable도 아니고, EEClass도 아닙니다. 게다가 더 이상한 건, Name 필드에 대한 offset 값이 0x08이고 담고 있는 값이 00000000024a77c8이므로 00007ff9`164857b0 주솟값에서 +08 위치에는 값이 보여야 하는데, 00007ff9`164857b0 앞/뒤로 덤프한 결과에는 해당 값이 나타나지 않았습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 00007ff9`16485678 00000000 00000000 00000000 00000000 00000000 00000000 00007ff9`16485690 00000000 00000000 00000000 00000000 00000000 00000000 00007ff9`164856a8 00000000 00000000 00000000 00000000 00000000 00000000 00007ff9`164856c0 164857b8 00007ff9 00000001 00000000 16485798 00007ff9 00007ff9`164856d8 00000000 00000000 00000003 00000001 00000000 00000038 00007ff9`164856f0 0000001b 00000000 00000000 00000000 00000000 00000000 00007ff9`16485708 00000000 00000000 00000000 00000000 00000000 00000000 00007ff9`16485720 00000000 00000000 00000000 00000000 00000000 00000000 00007ff9`16485738 00000000 00000000 00000000 00000000 00000000 00000000 00007ff9`16485750 00000000 00000000 00000000 00000000 164826e8 00007ff9 00007ff9`16485768 00000001 00000000 00000000 00000000 00772110 00000000 00007ff9`16485780 00000001 00000000 00000000 00000000 00000000 00000000 00007ff9`16485798 00000000 00000033 00000000 00000033 00000000 00000034 <span style='color: blue; font-weight: bold'>00007ff9`164857b0</span> 00000008 00000038 007946c0 00000000 00000000 00000000 00007ff9`164857c8 00000000 00000000 00000000 00000000 124a5ae8 00000000 00007ff9`164857e0 00000001 00000000 00050000 <span style='color: blue; font-weight: bold'>00000005</span> 76305b88 00007ff9 00007ff9`164857f8 00000001 00000000 164858e0 00007ff9 00000000 00000000 00007ff9`16485810 00000000 00000000 00000000 00000000 00000000 00000000 00007ff9`16485828 164858d0 00007ff9 00776d40 00000000 00000000 00000000 00007ff9`16485840 00000000 00000000 00000000 00000000 00000000 00000000 00007ff9`16485858 00000000 00000000 00000000 00000000 00000000 00000000 00007ff9`16485870 00000000 00000000 00000000 00000000 00737230 00000000 00007ff9`16485888 00000000 00000000 00000000 00000000 00000000 00000000 00007ff9`164858a0 00000004 00000000 00000000 00000000 007945a0 00000000 00007ff9`164858b8 007946c0 00000000 00000000 00000000 00000000 00000000 00007ff9`164858d0 736e6f43 41656c6f 00317070 00000000 00000000 00000000 00007ff9`164858e8 00000000 00000000 124a5af0 00000000 00000000 00000000 00007ff9`16485900 76305b88 00007ff9 00000004 00000000 16485a00 00007ff9 </pre> <br /> 그러니까... 아마도 저 offset 값이 static 필드의 값을 담고 있는 주솟값에 대한 의미는 아닌 듯합니다. 가만 생각해 보면 static 필드는 AppDomain 별로 분리되므로 전역적으로 단일 저장소에 있다고도 볼 수 없습니다. 다행히 이와 관련해 검색해 보면 다음과 같은 훌륭한 글이 나옵니다. ^^<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > RVA Static Fields ; <a target='tab' href='https://iobservable.net/blog/2013/03/03/rva-static-fields/'>https://iobservable.net/blog/2013/03/03/rva-static-fields/</a> </pre> <br /> 언제 기회 되면 저 글을 보면서 offset 값의 의미를 파악해봐야겠습니다. ^^<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
6775
(왼쪽의 숫자를 입력해야 합니다.)