2012年5月25日 星期五

Watch Process Memory Usage


To watch current process memory usage, we can use GetProcessMemoryInfo to get paging information.
It is sample code below.

    HANDLE hProcess = ::GetCurrentProcess();
    PROCESS_MEMORY_COUNTERS pmc;
    pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS);
    ::GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc));

    const int nBufSize = 512;
    TCHAR chBuf[nBufSize];
    ::ZeroMemory(chBuf,nBufSize);

    wsprintf(chBuf,_T("PageFaultCount: 0x%08X\n"), pmc.PageFaultCount );
    OutputDebugString(chBuf);

    wsprintf(chBuf,_T("PeakWorkingSetSize: 0x%08X\n"),
    pmc.PeakWorkingSetSize );
    OutputDebugString(chBuf);

    wsprintf(chBuf,_T("WorkingSetSize: 0x%08X\n"), pmc.WorkingSetSize );
    OutputDebugString(chBuf);

    wsprintf(chBuf,_T("QuotaPeakPagedPoolUsage: 0x%08X\n"),
    pmc.QuotaPeakPagedPoolUsage );
    OutputDebugString(chBuf);

    wsprintf(chBuf,_T("QuotaPagedPoolUsage: 0x%08X\n"),
    pmc.QuotaPagedPoolUsage );
    OutputDebugString(chBuf);

    wsprintf(chBuf,_T("QuotaPeakNonPagedPoolUsage: 0x%08X\n"),
    pmc.QuotaPeakNonPagedPoolUsage );
    OutputDebugString(chBuf);

    wsprintf(chBuf,_T("QuotaNonPagedPoolUsage: 0x%08X\n"),
    pmc.QuotaNonPagedPoolUsage );
    OutputDebugString(chBuf);

    wsprintf(chBuf,_T("PagefileUsage: 0x%08X\n"), pmc.PagefileUsage );
    OutputDebugString(chBuf);

    wsprintf(chBuf,_T("PeakPagefileUsage: 0x%08X\n"),
    pmc.PeakPagefileUsage );
    OutputDebugString(chBuf);

    CloseHandle(hProcess);


Reference:
http://msdn.microsoft.com/en-us/library/ms682050(v=VS.85).aspx
http://blog.csdn.net/caimouse/article/details/1947575

Work around link error:
error lnk2019: unresolved external symbol _getprocessmemoryinfo@12 referenced in function
http://lanvu.wordpress.com/tag/error-lnk2019-unresolved-external-symbol-_getprocessmemoryinfo12-referenced-in-function/

It seems doesn't work on windows ce.
https://groups.google.com/group/microsoft.public.pocketpc.developer/browse_thread/thread/22c685d1df9ec17d/e05ad892433d56aa?lnk=raot&pli=1

Structure of PROCESS_MEMORY_COUNTERS
http://blog.csdn.net/zhsp1029/article/details/3162048

沒有留言:

張貼留言