성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] 그냥 RSS Reader 기능과 약간의 UI 편의성 때문에 사용...
[이종효] 오래된 소프트웨어는 보안 위협이 되기도 합니다. 혹시 어떤 기능...
[정성태] @Keystroke IEEE의 문서를 소개해 주시다니... +_...
[손민수 (Keystroke)] 괜히 듀얼채널 구성할 때 한번에 같은 제품 사라고 하는 것이 아...
[정성태] 전각(Full-width)/반각(Half-width) 기능을 토...
[정성태] Vector에 대한 내용은 없습니다. Vector가 닷넷 BCL...
[orion] 글 읽고 찾아보니 디자인 타임에는 InitializeCompon...
[orion] 연휴 전에 재현 프로젝트 올리자 생각해 놓고 여의치 않아서 못 ...
[정성태] 아래의 글에 정리했으니 참고하세요. C# - Typed D...
[정성태] 간단한 재현 프로젝트라도 있을까요? 저런 식으로 설명만 해...
글쓰기
제목
이름
암호
전자우편
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# - 일정 크기를 할당하는 동안 GC를 (가능한) 멈추는 방법</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;' > 안녕하세요 GC를 일시적으로 중단시키는 방법이 있을까요? ; <a target='tab' href='https://www.sysnet.pe.kr/3/0/5672'>https://www.sysnet.pe.kr/3/0/5672</a> </pre> <br /> 중단시키는 것이 과연 바람직하냐...에 대한 논의를 떠나서 그냥 되는지 한번 보겠습니다. ^^<br /> <br /> 이를 위해서는 .NET 4.6/.NET Core 2.0부터 추가된 <a target='tab' href='https://www.sysnet.pe.kr/2/0/11869#12'>TryStartNoGCRegion</a> 메서드를 활용하면 되는데요, 특이하게 이 메서드로 하여금 확보할 수 있는 메모리의 크기는 "ephemeral <a target='tab' href='https://www.sysnet.pe.kr/2/0/13083'>segment</a>" 용량까지 가능합니다.<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;' > Ephemeral generations and segments ; <a target='tab' href='https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/fundamentals#ephemeral-generations-and-segments'>https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/fundamentals#ephemeral-generations-and-segments</a> </pre> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Workstation GC + 32비트: 16MB Workstation GC + 32비트: 256MB Server GC + 32비트: 64MB Server GC + 64비트: 4GB Server GC with > 4 logical CPUs + 32비트: 32MB Server GC with > 4 logical CPUs + 64비트: 2GB Server GC with > 8 logical CPUs + 32비트: 16MB Server GC with > 8 logical CPUs + 64비트: 1GB </pre> <br /> 이유는 알 수 없지만, TryStartNoGCRegion에 전달할 크기와 문서에 나온 ephemeral segment가 완전히 동일하지는 않습니다. 게다가 logical CPU 코어 수에 의존적인 부분도 있습니다. 따라서, 직접 테스트를 해보면 대략 다음과 같은 용량 정도를 테스트할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // 정확한 바이트 수는 무시합니다. // 16개 코어, 메모리 64GB 환경에서, Workstation + 32bit: 15MB // GC.TryStartNoGCRegion(15L * 1024 * 1024); Workstation + 64bit: 243MB // GC.TryStartNoGCRegion(243L * 1024 * 1024); Server + 32bit: 243MB // GC.TryStartNoGCRegion(243L * 1024 * 1024); Server + 64bit: 15GB // GC.TryStartNoGCRegion(15L * 1024 * 1024 * 1024); // 24개 코어, 메모리 64GB 환경에서, Server + 64bit: 22GB // GC.TryStartNoGCRegion(22L * 1024 * 1024 * 1024); </pre> <br /> 좀 더 정확한 규칙은 다양한 테스트를 해봐야 할 것 같지만, 일단 16개 코어에서 15GB까지 올라가는 것을 기준으로 테스트하겠습니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> 정리해 보면, Server GC를 사용하고 64비트 환경이라면 (제가 테스트한 시스템인 경우) 최대 15GB까지 미리 확보할 수 있고 따라서 그 정도 용량까지의 메모리 할당은 GC 수행 없이 가능하게 만들 수 있습니다.<br /> <br /> 실제로 테스트를 해볼까요? ^^<br /> <br /> 이를 위해 예제용 .NET 6 콘솔 프로젝트를 생성하고,<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.Runtime; internal class Program { static List<byte[]> s_buf = new List<byte[]>(); static void Main(string[] args) { Console.WriteLine($"64bit: {Environment.Is64BitProcess}"); Console.WriteLine($"Server GC: {GCSettings.IsServerGC}"); // 15GB 확보 bool result = GC.TryStartNoGCRegion(15L * 1024 * 1024 * 1024); Console.WriteLine($"TryStartNoGCRegion: {result}"); // SOH 7GB 할당 { long total = 7L * 1024 * 1024 * 1024; while (total > 0) { s_buf.Add(new byte[1024]); total -= 1024; } } GC.EndNoGCRegion(); } } </pre> <br /> <a target='tab' href='https://www.sysnet.pe.kr/2/0/12983'>runtimeconfig.template.json</a>에 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;' > { "configProperties": { "System.GC.Server": true, } } </pre> <br /> 기본 코드에 더해, GC 발생 여부를 알 수 있도록 다음과 같이 부가 코드를 넣을 수 있는데요,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Console.WriteLine($"# of GC: 0 == {GC.CollectionCount(0)}, 1 == {GC.CollectionCount(1)}, 2 == {GC.CollectionCount(2)}"); bool result = GC.TryStartNoGCRegion(14L * 1024 * 1024 * 1024); Console.WriteLine($"# of GC: 0 == {GC.CollectionCount(0)}, 1 == {GC.CollectionCount(1)}, 2 == {GC.CollectionCount(2)}"); Console.WriteLine($"TryStartNoGCRegion: {result}"); ...[생략]... Console.WriteLine($"# of GC: 0 == {GC.CollectionCount(0)}, 1 == {GC.CollectionCount(1)}, 2 == {GC.CollectionCount(2)}"); </pre> <br /> 이제 실행해보면 이런 결과를 얻게 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 64bit: True Server GC: True # of GC: 0 == 0, 1 == 0, 2 == 0 # of GC: 0 == 1, 1 == 1, 2 == 1 TryStartNoGCRegion: True # of GC: 0 == 1, 1 == 1, 2 == 1 </pre> <br /> 그러니까, TryStartNoGCRegion은 호출 시 기본적으로 GC 수집을 한번 수행하고 나서 메모리 확보 작업에 들어가는 것으로 유추할 수 있습니다. 또한 7GB의 메모리 할당이 이뤄지는 동안 한 번도 GC 수집이 발생하지 않은 것도 알 수 있습니다.<br /> <br /> 이 정도면, 그런대로 특수한 상황에서 쓸만한 결과일 듯합니다.<br /> <br /> (<a target='tab' href='https://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=1937&boardid=331301885'>첨부 파일은 이 글의 예제 코드를 포함</a>합니다.)<br /> <br /> <hr style='width: 50%' /><br /> <br /> 여러 차례 테스트를 해보면, 간혹 TryStartNoGCRegion/EndNoGCRegion 구간에 GC가 발생하는 경우가 있습니다. 그런 경우에는 EndNoGCRegion 호출 시 다음과 같은 예외가 발생하는데요,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Unhandled exception. System.InvalidOperationException: Garbage collection was induced in NoGCRegion mode at System.GC.EndNoGCRegion() at Program.Main(String[] args) in E:\...\Program.cs:line 37 </pre> <br /> 그러니까, GC가 발생했다는 것을 알려주는 듯한데 굳이 이걸 예외로 처리했어야 하나... 싶군요. 어쨌든 이와 관련해서는 문서에서도 살짝 언급은 하고 있습니다.<br /> <br /> <div style='BACKGROUND-COLOR: #ccffcc; padding: 10px 10px 5px 10px; MARGIN: 0px 10px 10px 10px; FONT-FAMILY: Malgun Gothic, Consolas, Verdana; COLOR: #005555'> // <a target='tab' href='https://learn.microsoft.com/en-us/dotnet/api/system.gc.trystartnogcregion'>https://learn.microsoft.com/en-us/dotnet/api/system.gc.trystartnogcregion</a><br /> <br /> you should only call the EndNoGCRegion method if the runtime is currently in no GC region latency mode.<br /> ...[생략]...<br /> you should not expect calls to EndNoGCRegion to succeed just because the first call to TryStartNoGCRegion succeeded.<br /> </div><br /> <br /> 그냥 반환값으로 해줬으면 더 좋았지 않았을까 싶습니다. ^^<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;' > Unhandled exception. System.InvalidOperationException: Allocated memory exceeds specified memory for NoGCRegion mode at System.GC.EndNoGCRegion() at Program.Main(String[] args) in E:\...\Program.cs:line 34 </pre> <br /> 역시나 이번에도 정보성인데 구역 내에서 TryStartNoGCRegion으로 알렸던 메모리를 초과해 사용했다는 정도입니다. 참고로, 이런 예외들을 try/catch로 감싸는 것도 가능하겠지만 다음과 같이 처리할 수도 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > if (GCSettings.LatencyMode == GCLatencyMode.NoGCRegion) { GC.EndNoGCRegion(); } </pre> <br /> 즉, 일단 위와 같은 예외가 발생하는 단계는 모두 GC가 중간에 호출이 되고, 그에 따라 "NoGCRegion" 모드가 풀리기 때문에 저렇게 호출하면 예외를 피할 수 있습니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> 한 가지 유의할 것이 있는데요, 제가 이 예제에서 15GB 정도의 메모리를 확보하라고 TryStartNoGCRegion에 넘겼지만, 실제로는 이때의 작업 관리자를 통해 <a target='tab' href='https://www.sysnet.pe.kr/2/0/1850'>(working set이 아닌) "Commit size"</a>를 확인해 보면 31GB 가까운 용량이 확보되는 것을 볼 수 있습니다.<br /> <br /> 이것을 windbg로 연결해 살펴 보면, "ephemeral segment"에 15GB, "Large object heap"에 15GB를 할당하는 것을 볼 수 있습니다. 결국 TryStartNoGCRegion은, 응용 프로그램이 할당할 메모리 유형이 SOH 또는 LOH 중 어디에 보관해야 하는지 정확히 알 수 없으므로 두 군데 모두 힙의 크기를 확보해 버리는 것입니다.<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;' > Preventing .NET Garbage Collections with the TryStartNoGCRegion API ; <a target='tab' href='https://mattwarren.org/2016/08/16/Preventing-dotNET-Garbage-Collections-with-the-TryStartNoGCRegion-API/'>https://mattwarren.org/2016/08/16/Preventing-dotNET-Garbage-Collections-with-the-TryStartNoGCRegion-API/</a> </pre> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
6408
(왼쪽의 숫자를 입력해야 합니다.)