'Process Memory'에 해당되는 글 1건

프로세스의 메모리 사용량가져오기

오랜만에 '개발' 카테고리에 글을..ㅋ

엄밀히 말하자면 프로세스의 워킹셋 사이즈를 가져오는 루틴이라고 봐야죠.

NT 4.0에서는 GetProcessMemoryInfo()을 호출할 때
"The program issued a command but the command lenght is incorrect" 라는 메세지 박스를 띄우는군요. Windows 업데이트문제인듯 한데..혹시 아시는 분 지혜를 부탁드릴께요~



#pragma comment(lib,"Psapi.lib")
#include "psapi.h"

DWORD GetUsingMemorySize(CString strProcessName)
{
 DWORD pid[1024];
 DWORD cb;
 EnumProcesses(pid,sizeof(DWORD)*1024,&cb);
 HMODULE hMo;
 CString strAll;
 for(int i = 0 ; i < 1024 ; ++i)
 {
  char szFileName[1024];
  HANDLE hProcess=OpenProcess(PROCESS_QUERY_INFORMATION |PROCESS_VM_READ,FALSE,pid[i]);
  if(!hProcess)
   continue;
  BOOL bb=EnumProcessModules(hProcess,&hMo,sizeof(hMo),&cb);
  if(!bb)
  {
   DWORD error=GetLastError();
   LPVOID lpMsgBuf;
   FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
      |FORMAT_MESSAGE_FROM_SYSTEM
      |FORMAT_MESSAGE_IGNORE_INSERTS
      , NULL,error,0,(LPSTR)&lpMsgBuf,0,NULL);
   CString strLastErrMsg=static_cast<LPCSTR>(lpMsgBuf);
   LocalFree(lpMsgBuf);
   CloseHandle(hProcess);
   continue;
  }
  GetModuleFileNameEx(hProcess,hMo,szFileName,1024);
  CString strProcName(szFileName);
  if(strProcName.Find(strProcessName) != -1 )
  {
   PROCESS_MEMORY_COUNTERS mem;
   GetProcessMemoryInfo(hProcess,&mem,sizeof(PROCESS_MEMORY_COUNTERS));
   CloseHandle(hProcess);
   return mem.WorkingSetSize;
  }
  CloseHandle(hProcess);
 }
 return NULL;
}

'개발' 카테고리의 다른 글

응용 프로그램을 위한 최상의 사용자 환경을 만드는 방법  (0) 2008.02.26
simpleAdo 2.10  (0) 2008.02.21
프로세스의 메모리 사용량가져오기  (2) 2007.09.27
TCP/IP wouldblock 처리  (1) 2007.07.27
Shock Sticker  (1) 2007.07.12
SciTE Editor 한글화하기..  (2) 2007.07.03

Trackbacks 0 / Comments 2

  • Z 2007.10.02 14:15 신고

    ZwQueryInformationProcess 함수를 알아보세요~... Native API 를 사용할 줄 안다면 문제가 뭔지 파악하고 구현해 볼 수 있을 듯.

  • Z 2007.10.02 14:18 신고

    BOOL
    WINAPI
    GetProcessMemoryInfo (
    HANDLE hProcess,
    PPROCESS_MEMORY_COUNTERS ppsmemCounters,
    DWORD cb
    )

    /*++

    Routine Description:

    This function returns all the PSVM_COUNTERS for a process.

    Arguments:

    hProcess - Handle for the process being queried.

    ppsmemCounters - Points to buffer that will receive the PROCESS_MEMORY_COUNTERS.

    cb - size of ppsmemCounters

    Return Value:

    The return value is TRUE or FALSE.

    --*/

    {
    NTSTATUS Status;
    VM_COUNTERS VmCounters;

    // Try to feel if the ptr passed is NULL and if not,
    // is it long enough for us.

    try {
    ppsmemCounters->PeakPagefileUsage = 0;
    }
    except (EXCEPTION_EXECUTE_HANDLER) {
    SetLastError( RtlNtStatusToDosError( GetExceptionCode() ) );
    return(FALSE);
    }

    if (cb < sizeof(PROCESS_MEMORY_COUNTERS)) {
    SetLastError( ERROR_INSUFFICIENT_BUFFER );
    return(FALSE);
    }

    Status = NtQueryInformationProcess(
    hProcess,
    ProcessVmCounters,
    &VmCounters,
    sizeof(VM_COUNTERS),
    NULL
    );

    if ( !NT_SUCCESS(Status) )
    {
    SetLastError( RtlNtStatusToDosError( Status ) );
    return( FALSE );
    }

    ppsmemCounters->cb = sizeof(PROCESS_MEMORY_COUNTERS);
    ppsmemCounters->PageFaultCount = VmCounters.PageFaultCount;
    ppsmemCounters->PeakWorkingSetSize = VmCounters.PeakWorkingSetSize;
    ppsmemCounters->WorkingSetSize = VmCounters.WorkingSetSize;
    ppsmemCounters->QuotaPeakPagedPoolUsage = VmCounters.QuotaPeakPagedPoolUsage;
    ppsmemCounters->QuotaPagedPoolUsage = VmCounters.QuotaPagedPoolUsage;
    ppsmemCounters->QuotaPeakNonPagedPoolUsage= VmCounters.QuotaPeakNonPagedPoolUsage;
    ppsmemCounters->QuotaNonPagedPoolUsage = VmCounters.QuotaNonPagedPoolUsage;
    ppsmemCounters->PagefileUsage = VmCounters.PagefileUsage;
    ppsmemCounters->PeakPagefileUsage = VmCounters.PeakPagefileUsage;

    return(TRUE);
    }

Leave Comments