Microsoft MVP성태의 닷넷 이야기
Linux: 57. C# - 리눅스 프로세스 메모리 정보 [링크 복사], [링크+제목 복사]
조회: 4302
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일

C# - 리눅스 프로세스 메모리 정보

리눅스 환경에서 메모리 정보를 구해볼까요? ^^ 우선, 간단하게는 "ps" 명령어의 결과로 구하는 메모리 정보가 있는데요,

$ ps -o vsz,rss -p 1950
   VSZ   RSS
  2300   108

What is RSS and VSZ in Linux memory management
; https://stackoverflow.com/questions/7880784/what-is-rss-and-vsz-in-linux-memory-management

C#으로는 이렇게 코딩할 수 있습니다.

private static (long vsz, long rss) GetMemoryInfoFromPS(int pid)
{
    long vsz = 0;
    long rss = 0;

    string cmd = $"ps -o vsz,rss -p {pid} --no-headers";

    ProcessStartInfo psi = new ProcessStartInfo();
    psi.UseShellExecute = false;
    psi.FileName = "bash";
    psi.Arguments = "-c \"" + cmd + "\"";
    psi.RedirectStandardOutput = true;

    Process? runningProcess = Process.Start(psi);
    if (runningProcess == null)
    {
        return (0, 0);
    }

    runningProcess.WaitForExit();

    string text = runningProcess.StandardOutput.ReadToEnd().Trim();
    string[] items = text.Split();
    if (items.Length != 2)
    {
        return (0, 0);
    }

    long.TryParse(items[0], out vsz);
    long.TryParse(items[1], out rss);

    return (vsz, rss);
}

위에서 구한 vsz, rss는 윈도우와 비교하면 "Process Explorer"의 virtual size와 working set에 해당합니다. 그런데 이 정보들은 굳이 ps 명령어의 결과로 추출할 필요없이 Process 개체를 통해 간단하게 구할 수 있습니다.

Process process = Process.GetCurrentProcess();
long vsz = process.VirtualMemorySize64;
long vszKB = vsz / 1024;

long rss = process.WorkingSet64;
long rssKB = rss / 1024;

vszKB, rssKB와 ps 출력의 vsz, rss 값은 정확히 일치합니다.




리눅스의 경우, VMMap과 같은 정보를 /proc/[pid]/maps를 통해 구하는 것도 가능합니다.

$ cat /proc/3035/maps
564b14faa000-564b14fb6000 r--p 00000000 08:30 89216                      /usr/share/dotnet/dotnet
564b14fb6000-564b14fcc000 r-xp 0000c000 08:30 89216                      /usr/share/dotnet/dotnet
564b14fcc000-564b14fcd000 r--p 00022000 08:30 89216                      /usr/share/dotnet/dotnet
564b14fcd000-564b14fce000 rw-p 00023000 08:30 89216                      /usr/share/dotnet/dotnet
564b15973000-564b15b80000 rw-p 00000000 00:00 0                          [heap]
7f3b58000000-7f3b58021000 rw-p 00000000 00:00 0
...[생략]...
7f7c98e9f000-7f7c98ea0000 r--p 00000000 08:30 20985                      /usr/lib/x86_64-linux-gnu/ld-2.31.so
7f7c98ea0000-7f7c98ec3000 r-xp 00001000 08:30 20985                      /usr/lib/x86_64-linux-gnu/ld-2.31.so
7f7c98ec3000-7f7c98ecb000 r--p 00024000 08:30 20985                      /usr/lib/x86_64-linux-gnu/ld-2.31.so
7f7c98ecb000-7f7c98ecc000 rw-s 00471000 00:01 1180                       /memfd:doublemapper (deleted)
7f7c98ecc000-7f7c98ecd000 r--p 0002c000 08:30 20985                      /usr/lib/x86_64-linux-gnu/ld-2.31.so
7f7c98ecd000-7f7c98ece000 rw-p 0002d000 08:30 20985                      /usr/lib/x86_64-linux-gnu/ld-2.31.so
7f7c98ece000-7f7c98ecf000 rw-p 00000000 00:00 0
7ffdb8167000-7ffdb8188000 rw-p 00000000 00:00 0                          [stack]
7ffdb819d000-7ffdb81a1000 r--p 00000000 00:00 0                          [vvar]
7ffdb81a1000-7ffdb81a2000 r-xp 00000000 00:00 0                          [vdso]

예상할 수 있겠지만, 개별 라인(예를 들어, "7f7c98e9f000-7f7c98ea0000")에 해당하는 주소 범위를 모두 더하면 정확히 (이전 소스 코드에서 구한) vsz에 해당하는 크기가 나옵니다.

혹시 저 영역 중에 GC Heap은 어디에 포함될까요? ^^ 아래의 글을 보면,

/proc/pid/maps : 파일 프로세스의 메모리 공간
; https://sosal.kr/328

/proc/<pid>/maps
; https://umbum.dev/140

[heap] 영역은 C/C++ 코드에서 new/malloc 등으로 할당한 공간이라고 하는데요, 그렇다면 GC Heap도 거기에 포함되지 않을까요?

이를 검증하기 위해 참조 개체를 fixed 상태로 만들어 구한 주소를,

Program pg = new Program();
fixed (int* ptr = &pg._dummy)
{
    IntPtr addr = new nint(ptr);

    // addr 주소를 /proc/pid/maps와 비교
}

찾아보면 "[heap]"에 속하지 않은 다른 범위의 주소가 나옵니다. 일례로 아래와 같은 범위로,

7ebc12800000-7ebc12831000 rw-p 00000000 00:00 0

메모리 범위의 크기를 보면, 200,704가 나오는데요, 약 196KB에 해당합니다. 또한, 이 영역을 /proc/pid/maps의 전체 출력에서 찾아보면,

56212274d000-562122759000 r--p 00000000 08:20 36634                      /usr/share/dotnet/dotnet
562122759000-56212276f000 r-xp 0000c000 08:20 36634                      /usr/share/dotnet/dotnet
56212276f000-562122770000 r--p 00022000 08:20 36634                      /usr/share/dotnet/dotnet
562122770000-562122771000 rw-p 00023000 08:20 36634                      /usr/share/dotnet/dotnet
562123620000-562123815000 rw-p 00000000 00:00 0                          [heap]
7ebb64000000-7ebb64021000 rw-p 00000000 00:00 0 
7ebb64021000-7ebb68000000 ---p 00000000 00:00 0 
7ebb6c000000-7ebb6c021000 rw-p 00000000 00:00 0 
7ebb6c021000-7ebb70000000 ---p 00000000 00:00 0 
7ebb70000000-7ebb70068000 rw-p 00000000 00:00 0 
7ebb70068000-7ebb74000000 ---p 00000000 00:00 0 
7ebb74000000-7ebb74021000 rw-p 00000000 00:00 0 
7ebb74021000-7ebb78000000 ---p 00000000 00:00 0 
7ebb7a555000-7ebb7a556000 ---p 00000000 00:00 0 
7ebb7a556000-7ebb7ad56000 rw-p 00000000 00:00 0 
7ebb7ad56000-7ebb7ad57000 ---p 00000000 00:00 0 
7ebb7ad57000-7ebb7b561000 rw-p 00000000 00:00 0 
7ebb7b561000-7ebb83556000 ---p 00000000 00:00 0 
7ebb83556000-7ebb83561000 rw-p 00000000 00:00 0 
7ebb83561000-7ebb8b556000 ---p 00000000 00:00 0 
7ebb8b556000-7ebb8b557000 rw-p 00000000 00:00 0 
7ebb8b557000-7ebb8b576000 ---p 00000000 00:00 0 
7ebb8b576000-7ebb8b57b000 rw-p 00000000 00:00 0 
7ebb8b57b000-7ebb8f575000 ---p 00000000 00:00 0 
7ebb8f575000-7ebb8f576000 rw-p 00000000 00:00 0 
7ebb8f576000-7ebb8f585000 ---p 00000000 00:00 0 
7ebb8f585000-7ebb8f587000 rw-p 00000000 00:00 0 
7ebb8f587000-7ebc10000000 ---p 00000000 00:00 0 
7ebc10000000-7ebc10011000 rw-p 00000000 00:00 0 
7ebc10011000-7ebc12000000 ---p 00000000 00:00 0 
7ebc12000000-7ebc12001000 rw-p 00000000 00:00 0 
7ebc12001000-7ebc12400000 ---p 00000000 00:00 0 
7ebc12400000-7ebc12401000 rw-p 00000000 00:00 0 
7ebc12401000-7ebc12800000 ---p 00000000 00:00 0  
7ebc12800000-7ebc12831000 rw-p 00000000 00:00 0
7ebc12831000-7ebc12c00000 ---p 00000000 00:00 0  
7ebc12c00000-7ebc12c01000 rw-p 00000000 00:00 0 
7ebc12c01000-7efc10000000 ---p 00000000 00:00 0 
7efc10000000-7efc10021000 rw-p 00000000 00:00 0 
7efc10021000-7efc14000000 ---p 00000000 00:00 0 
7efc14000000-7efc14021000 rw-p 00000000 00:00 0 
7efc14021000-7efc18000000 ---p 00000000 00:00 0 
7efc18000000-7efc18021000 rw-p 00000000 00:00 0 
7efc18021000-7efc1c000000 ---p 00000000 00:00 0 
7efc1c000000-7efc1c021000 rw-p 00000000 00:00 0 
7efc1c021000-7efc20000000 ---p 00000000 00:00 0 
7efc20000000-7efc20021000 rw-p 00000000 00:00 0 
7efc20021000-7efc24000000 ---p 00000000 00:00 0 
...[생략]...

앞 뒤로 다수의 메모리 블록이 연속해서 메모리가 잡힌 것을 확인할 수 있습니다. 아쉽군요, ^^ 딱히 GC Heap 영역이라고 특정 지을 수 있는 방법이 없습니다.

(첨부 파일은 이 글의 예제 코드를 포함합니다.)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 1/30/2023]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13607정성태4/25/2024199닷넷: 2248.C# - 인터페이스 타입의 다중 포인터를 인자로 갖는 C/C++ 함수 연동
13606정성태4/24/2024218닷넷: 2247. C# - tensorflow 연동 (MNIST 예제)파일 다운로드1
13605정성태4/23/2024466닷넷: 2246. C# - Python.NET을 이용한 파이썬 소스코드 연동파일 다운로드1
13604정성태4/22/2024517오류 유형: 901. Visual Studio - Unable to set the next statement. Set next statement cannot be used in '[Exception]' call stack frames.
13603정성태4/21/2024738닷넷: 2245. C# - IronPython을 이용한 파이썬 소스코드 연동파일 다운로드1
13602정성태4/20/2024817닷넷: 2244. C# - PCM 오디오 데이터를 연속(Streaming) 재생 (Windows Multimedia)파일 다운로드1
13601정성태4/19/2024862닷넷: 2243. C# - PCM 사운드 재생(NAudio)파일 다운로드1
13600정성태4/18/2024900닷넷: 2242. C# - 관리 스레드와 비관리 스레드
13599정성태4/17/2024876닷넷: 2241. C# - WAV 파일의 PCM 사운드 재생(Windows Multimedia)파일 다운로드1
13598정성태4/16/2024903닷넷: 2240. C# - WAV 파일 포맷 + LIST 헤더파일 다운로드2
13597정성태4/15/2024885닷넷: 2239. C# - WAV 파일의 PCM 데이터 생성 및 출력파일 다운로드1
13596정성태4/14/20241077닷넷: 2238. C# - WAV 기본 파일 포맷파일 다운로드1
13595정성태4/13/20241055닷넷: 2237. C# - Audio 장치 열기 (Windows Multimedia, NAudio)파일 다운로드1
13594정성태4/12/20241070닷넷: 2236. C# - Audio 장치 열람 (Windows Multimedia, NAudio)파일 다운로드1
13593정성태4/8/20241088닷넷: 2235. MSBuild - AccelerateBuildsInVisualStudio 옵션
13592정성태4/2/20241226C/C++: 165. CLion으로 만든 Rust Win32 DLL을 C#과 연동
13591정성태4/2/20241201닷넷: 2234. C# - WPF 응용 프로그램에 Blazor App 통합파일 다운로드1
13590정성태3/31/20241079Linux: 70. Python - uwsgi 응용 프로그램이 k8s 환경에서 OOM 발생하는 문제
13589정성태3/29/20241158닷넷: 2233. C# - 프로세스 CPU 사용량을 나타내는 성능 카운터와 Win32 API파일 다운로드1
13588정성태3/28/20241272닷넷: 2232. C# - Unity + 닷넷 App(WinForms/WPF) 간의 Named Pipe 통신 [2]파일 다운로드1
13587정성태3/27/20241172오류 유형: 900. Windows Update 오류 - 8024402C, 80070643
13586정성태3/27/20241340Windows: 263. Windows - 복구 파티션(Recovery Partition) 용량을 늘리는 방법
13585정성태3/26/20241145Windows: 262. PerformanceCounter의 InstanceName에 pid를 추가한 "Process V2"
13584정성태3/26/20241273개발 환경 구성: 708. Unity3D - C# Windows Forms / WPF Application에 통합하는 방법파일 다운로드1
13583정성태3/25/20241489Windows: 261. CPU Utilization이 100% 넘는 경우를 성능 카운터로 확인하는 방법
[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...