선언:

C#

[DllImport("psapi")]
public static extern Boolean GetProcessMemoryInfo(IntPtr hProcess, ref COMPATIBLE_PROCESS_MEMORY_COUNTERS cpmc, Int32 cb);
[DllImport("psapi")]
public static extern Boolean GetProcessMemoryInfo(IntPtr hProcess, ref PROCESS_MEMORY_COUNTERS pmc, Int32 cb);
[DllImport("psapi")]
public static extern Boolean GetProcessMemoryInfo(IntPtr hProcess, ref PROCESS_MEMORY_COUNTERS_EX pmcex, Int32 cb);


VB.NET

<DllImport("psapi")> _
Public Shared Function GetProcessMemoryInfo(hProcess As IntPtr, ByRef cpmc As COMPATIBLE_PROCESS_MEMORY_COUNTERS, cb As Int32) As [Boolean]
End Function
<DllImport("psapi")> _
Public Shared Function GetProcessMemoryInfo(hProcess As IntPtr, ByRef pmc As PROCESS_MEMORY_COUNTERS, cb As Int32) As [Boolean]
End Function
<DllImport("psapi")> _
Public Shared Function GetProcessMemoryInfo(hProcess As IntPtr, ByRef pmcex As PROCESS_MEMORY_COUNTERS_EX, cb As Int32) As [Boolean]
End Function




사용 예제:

C#

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace ApiReference {
    class ApiExample {
        public struct PROCESS_MEMORY_COUNTERS {
            public UInt32 cb;
            public UInt32 PageFaultCount;
            public UIntPtr PeakWorkingSetSize;
            public UIntPtr WorkingSetSize;
            public UIntPtr QuotaPeakPagedPoolUsage;
            public UIntPtr QuotaPagedPoolUsage;
            public UIntPtr QuotaPeakNonPagedPoolUsage;
            public UIntPtr QuotaNonPagedPoolUsage;
            public UIntPtr PagefileUsage;
            public UIntPtr PeakPagefileUsage;
        }
        public struct PROCESS_MEMORY_COUNTERS_EX {
            public UInt32 cb;
            public UInt32 PageFaultCount;
            public UIntPtr PeakWorkingSetSize;
            public UIntPtr WorkingSetSize;
            public UIntPtr QuotaPeakPagedPoolUsage;
            public UIntPtr QuotaPagedPoolUsage;
            public UIntPtr QuotaPeakNonPagedPoolUsage;
            public UIntPtr QuotaNonPagedPoolUsage;
            public UIntPtr PagefileUsage;
            public UIntPtr PeakPagefileUsage;
            public UIntPtr PrivateUsage;
        }
        public struct COMPATIBLE_PROCESS_MEMORY_COUNTERS {
            public COMPATIBLE_PROCESS_MEMORY_COUNTERS(PROCESS_MEMORY_COUNTERS pmc) {
                cb = pmc.cb;
                PageFaultCount = pmc.PageFaultCount;
                PeakWorkingSetSize = pmc.PeakWorkingSetSize;
                WorkingSetSize = pmc.WorkingSetSize;
                QuotaPeakPagedPoolUsage = pmc.QuotaPeakPagedPoolUsage;
                QuotaPagedPoolUsage = pmc.QuotaPagedPoolUsage;
                QuotaPeakNonPagedPoolUsage = pmc.QuotaPeakNonPagedPoolUsage;
                QuotaNonPagedPoolUsage = pmc.QuotaNonPagedPoolUsage;
                PagefileUsage = pmc.PagefileUsage;
                PeakPagefileUsage = pmc.PeakPagefileUsage;
                PrivateUsage = UIntPtr.Zero;
            }
            public COMPATIBLE_PROCESS_MEMORY_COUNTERS(PROCESS_MEMORY_COUNTERS_EX pmcex) {
                cb = pmcex.cb;
                PageFaultCount = pmcex.PageFaultCount;
                PeakWorkingSetSize = pmcex.PeakWorkingSetSize;
                WorkingSetSize = pmcex.WorkingSetSize;
                QuotaPeakPagedPoolUsage = pmcex.QuotaPeakPagedPoolUsage;
                QuotaPagedPoolUsage = pmcex.QuotaPagedPoolUsage;
                QuotaPeakNonPagedPoolUsage = pmcex.QuotaPeakNonPagedPoolUsage;
                QuotaNonPagedPoolUsage = pmcex.QuotaNonPagedPoolUsage;
                PagefileUsage = pmcex.PagefileUsage;
                PeakPagefileUsage = pmcex.PeakPagefileUsage;
                PrivateUsage = pmcex.PrivateUsage;
            }
            public UInt32 cb;
            public UInt32 PageFaultCount;
            public UIntPtr PeakWorkingSetSize;
            public UIntPtr WorkingSetSize;
            public UIntPtr QuotaPeakPagedPoolUsage;
            public UIntPtr QuotaPagedPoolUsage;
            public UIntPtr QuotaPeakNonPagedPoolUsage;
            public UIntPtr QuotaNonPagedPoolUsage;
            public UIntPtr PagefileUsage;
            public UIntPtr PeakPagefileUsage;
            public UIntPtr PrivateUsage;
        }

        [DllImport("psapi")]
        public static extern Boolean GetProcessMemoryInfo(IntPtr hProcess, ref COMPATIBLE_PROCESS_MEMORY_COUNTERS cpmc, Int32 cb);
        
        public static void Main(string[] args) {
            COMPATIBLE_PROCESS_MEMORY_COUNTERS cpmc = default(COMPATIBLE_PROCESS_MEMORY_COUNTERS);
            
            // 프로세스
            Console.Write("메모리를 조사할 프로세스의 식별자를 입력하세요: ");
        enterAgain:
            Int32 pid;
            if ( !Int32.TryParse(Console.ReadLine(), out pid) ) {
                Console.WriteLine("다시 입력하세요");
                goto enterAgain;
            }
            
            Process p = Process.GetProcessById(pid);
            if ( p == null ) {
                Console.WriteLine("입력한 식별자를 가진 프로세스가 없습니다. 다시 입력하세요");
                goto enterAgain;
            }
            
            
            Console.Write("유형을 선택하세요. (0 : 기본, 0이 아닌 값: 확장된 정보): ");
        enterTypeAgain:
            Int32 type;
            if ( !Int32.TryParse(Console.ReadLine(), out type) ) {
                Console.WriteLine("다시 입력하세요");
                goto enterTypeAgain;
            }
            
            if ( type == 0 )
                cpmc.cb = (UInt32) Marshal.SizeOf(typeof(PROCESS_MEMORY_COUNTERS));
            else
                cpmc.cb = (UInt32) Marshal.SizeOf(typeof(PROCESS_MEMORY_COUNTERS_EX));
            
            if ( GetProcessMemoryInfo(p.Handle, ref cpmc, (Int32) cpmc.cb) ) {
                Console.WriteLine("메모리 정보 가져오기 성공!");
                Console.WriteLine("PageFaultCount              = {0}", cpmc.PageFaultCount);
                Console.WriteLine("PeakWorkingSetSize          = {0}", cpmc.PeakWorkingSetSize);
                Console.WriteLine("WorkingSetSize              = {0}", cpmc.WorkingSetSize);
                Console.WriteLine("QuotaPeakPagedPoolUsage     = {0}", cpmc.QuotaPeakPagedPoolUsage);
                Console.WriteLine("QuotaPagedPoolUsage         = {0}", cpmc.QuotaPagedPoolUsage);
                Console.WriteLine("QuotaPeakNonPagedPoolUsage  = {0}", cpmc.QuotaPeakNonPagedPoolUsage);
                Console.WriteLine("QuotaNonPagedPoolUsage      = {0}", cpmc.QuotaNonPagedPoolUsage);
                Console.WriteLine("PagefileUsage               = {0}", cpmc.PagefileUsage);
                Console.WriteLine("PeakPagefileUsage           = {0}", cpmc.PeakPagefileUsage);
                Console.WriteLine("PrivateUsage                = {0}", cpmc.PrivateUsage);
            } else
                Console.WriteLine("메모리 정보 가져오기 실패!");
            
            Console.ReadKey(true);
        }
    }
}


VB.NET

Imports System
Imports System.Diagnostics
Imports System.Runtime.InteropServices

Namespace ApiReference
    Class ApiExample
        Public Structure PROCESS_MEMORY_COUNTERS
            Public cb As UInt32
            Public PageFaultCount As UInt32
            Public PeakWorkingSetSize As UIntPtr
            Public WorkingSetSize As UIntPtr
            Public QuotaPeakPagedPoolUsage As UIntPtr
            Public QuotaPagedPoolUsage As UIntPtr
            Public QuotaPeakNonPagedPoolUsage As UIntPtr
            Public QuotaNonPagedPoolUsage As UIntPtr
            Public PagefileUsage As UIntPtr
            Public PeakPagefileUsage As UIntPtr
        End Structure
        Public Structure PROCESS_MEMORY_COUNTERS_EX
            Public cb As UInt32
            Public PageFaultCount As UInt32
            Public PeakWorkingSetSize As UIntPtr
            Public WorkingSetSize As UIntPtr
            Public QuotaPeakPagedPoolUsage As UIntPtr
            Public QuotaPagedPoolUsage As UIntPtr
            Public QuotaPeakNonPagedPoolUsage As UIntPtr
            Public QuotaNonPagedPoolUsage As UIntPtr
            Public PagefileUsage As UIntPtr
            Public PeakPagefileUsage As UIntPtr
            Public PrivateUsage As UIntPtr
        End Structure
        Public Structure COMPATIBLE_PROCESS_MEMORY_COUNTERS
            Public Sub New(pmc As PROCESS_MEMORY_COUNTERS)
                cb = pmc.cb
                PageFaultCount = pmc.PageFaultCount
                PeakWorkingSetSize = pmc.PeakWorkingSetSize
                WorkingSetSize = pmc.WorkingSetSize
                QuotaPeakPagedPoolUsage = pmc.QuotaPeakPagedPoolUsage
                QuotaPagedPoolUsage = pmc.QuotaPagedPoolUsage
                QuotaPeakNonPagedPoolUsage = pmc.QuotaPeakNonPagedPoolUsage
                QuotaNonPagedPoolUsage = pmc.QuotaNonPagedPoolUsage
                PagefileUsage = pmc.PagefileUsage
                PeakPagefileUsage = pmc.PeakPagefileUsage
                PrivateUsage = UIntPtr.Zero
            End Sub
            Public Sub New(pmcex As PROCESS_MEMORY_COUNTERS_EX)
                cb = pmcex.cb
                PageFaultCount = pmcex.PageFaultCount
                PeakWorkingSetSize = pmcex.PeakWorkingSetSize
                WorkingSetSize = pmcex.WorkingSetSize
                QuotaPeakPagedPoolUsage = pmcex.QuotaPeakPagedPoolUsage
                QuotaPagedPoolUsage = pmcex.QuotaPagedPoolUsage
                QuotaPeakNonPagedPoolUsage = pmcex.QuotaPeakNonPagedPoolUsage
                QuotaNonPagedPoolUsage = pmcex.QuotaNonPagedPoolUsage
                PagefileUsage = pmcex.PagefileUsage
                PeakPagefileUsage = pmcex.PeakPagefileUsage
                PrivateUsage = pmcex.PrivateUsage
            End Sub
            Public cb As UInt32
            Public PageFaultCount As UInt32
            Public PeakWorkingSetSize As UIntPtr
            Public WorkingSetSize As UIntPtr
            Public QuotaPeakPagedPoolUsage As UIntPtr
            Public QuotaPagedPoolUsage As UIntPtr
            Public QuotaPeakNonPagedPoolUsage As UIntPtr
            Public QuotaNonPagedPoolUsage As UIntPtr
            Public PagefileUsage As UIntPtr
            Public PeakPagefileUsage As UIntPtr
            Public PrivateUsage As UIntPtr
        End Structure

        <DllImport("psapi")> _
        Public Shared Function GetProcessMemoryInfo(hProcess As IntPtr, ByRef cpmc As COMPATIBLE_PROCESS_MEMORY_COUNTERS, cb As Int32) As [Boolean]
        End Function

        Public Shared Sub Main(args As String())
            Dim cpmc As COMPATIBLE_PROCESS_MEMORY_COUNTERS = Nothing

            ' 프로세스
            Console.Write("메모리를 조사할 프로세스의 식별자를 입력하세요: ")
            enterAgain:
            Dim pid As Int32
            If Not Int32.TryParse(Console.ReadLine(), pid) Then
                Console.WriteLine("다시 입력하세요")
                GoTo enterAgain
            End If

            Dim p As Process = Process.GetProcessById(pid)
            If p Is Nothing Then
                Console.WriteLine("입력한 식별자를 가진 프로세스가 없습니다. 다시 입력하세요")
                GoTo enterAgain
            End If


            Console.Write("유형을 선택하세요. (0 : 기본, 0이 아닌 값: 확장된 정보): ")
            enterTypeAgain:
            Dim type As Int32
            If Not Int32.TryParse(Console.ReadLine(), type) Then
                Console.WriteLine("다시 입력하세요")
                GoTo enterTypeAgain
            End If

            If type = 0 Then
                cpmc.cb = CType(Marshal.SizeOf(GetType(PROCESS_MEMORY_COUNTERS)), UInt32)
            Else
                cpmc.cb = CType(Marshal.SizeOf(GetType(PROCESS_MEMORY_COUNTERS_EX)), UInt32)
            End If

            If GetProcessMemoryInfo(p.Handle, cpmc, CType(cpmc.cb, Int32)) Then
                Console.WriteLine("메모리 정보 가져오기 성공!")
                Console.WriteLine("PageFaultCount              = {0}", cpmc.PageFaultCount)
                Console.WriteLine("PeakWorkingSetSize          = {0}", cpmc.PeakWorkingSetSize)
                Console.WriteLine("WorkingSetSize              = {0}", cpmc.WorkingSetSize)
                Console.WriteLine("QuotaPeakPagedPoolUsage     = {0}", cpmc.QuotaPeakPagedPoolUsage)
                Console.WriteLine("QuotaPagedPoolUsage         = {0}", cpmc.QuotaPagedPoolUsage)
                Console.WriteLine("QuotaPeakNonPagedPoolUsage  = {0}", cpmc.QuotaPeakNonPagedPoolUsage)
                Console.WriteLine("QuotaNonPagedPoolUsage      = {0}", cpmc.QuotaNonPagedPoolUsage)
                Console.WriteLine("PagefileUsage               = {0}", cpmc.PagefileUsage)
                Console.WriteLine("PeakPagefileUsage           = {0}", cpmc.PeakPagefileUsage)
                Console.WriteLine("PrivateUsage                = {0}", cpmc.PrivateUsage)
            Else
                Console.WriteLine("메모리 정보 가져오기 실패!")
            End If

            Console.ReadKey(True)
        End Sub
    End Class
End Namespace




예제 실행 결과:



매개 변수 설명:

hProcess - 메모리 정보를 가져올 대상 프로세스의 핸들을 입력합니다.

pmc, pmcex, cpmc - 메모리 정보가 저장될 구조체를 입력합니다.

cb - 메모리 정보가 저장될 구조체의 크기를 입력합니다.




API 설명:

프로세스의 메모리 정보를 가져옵니다.




참고:

GetProcessMemoryInfo (MSDN)




요구 사항:

클라이언트: Windows XP+

서버: Windows Server 2003+




비고:

이 API가 성공적으로 호출되기 위해서는 프로세스 핸들을 PROCESS_QUERY_INFORMATION 또는 PROCESS_QUERY_LIMITED_INFORMATION 권한과 PROCESS_VM_READ 권한을 이용하여 핸들을 열어야 합니다.

정보가 담길 구조체의 cb 필드에도 구조체의 크기를 입력해야 합니다.

Windows XP 및 Windows Server 2003 운영체제에서는 반드시 PROCESS_QUERY_INFORMATION 권한과 PROCESS_VM_READ 권한이 있어야 합니다.


'API Reference' 카테고리의 다른 글

53. FindWindow  (1) 2015.01.04
52. CommandLineToArgvW  (0) 2014.12.17
50. PROCESS_MEMORY_COUNTERS_EX  (0) 2014.12.12
49. PROCESS_MEMORY_COUNTERS  (0) 2014.12.11
48. GetDiskFreeSpace  (0) 2014.12.09

+ Recent posts