본문 바로가기
2_ 바삭바삭 프로그래밍/C# and Visual C++

MFC - Process에서 메모리 사용량 알아내기

by 준환이형님_ 2011. 4. 6.

아래의 코드를 참고하며 MFC에서 프로세스의 사용량을 알 수 있습니다 ^ㅡ^

MFC관련 포스팅을 자주 하게 되어 기뻐요ㅋ 그동안 소홀해서 맘에 걸렸는데 ㅋㅋㅋ

출처 : http://www.codeguru.com/forum/


#include <iostream> #include <iomanip> #include <psapi.h> // Add 'psapi.lib' to your linker options int main() { // Open current process HANDLE hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, ::GetCurrentProcessId()); if(hProcess) { PROCESS_MEMORY_COUNTERS ProcessMemoryCounters; memset(&ProcessMemoryCounters, 0, sizeof(ProcessMemoryCounters)); // Set size of structure ProcessMemoryCounters.cb = sizeof(ProcessMemoryCounters); // Get memory usage if(::GetProcessMemoryInfo(hProcess, &ProcessMemoryCounters, sizeof(ProcessMemoryCounters)) == TRUE) { std::cout << std::setfill('0') << std::hex << "PageFaultCount: 0x" << std::setw(8) << ProcessMemoryCounters.PageFaultCount << std::endl << "PeakWorkingSetSize: 0x" << std::setw(8) << ProcessMemoryCounters.PeakWorkingSetSize << std::endl << "WorkingSetSize: 0x" << std::setw(8) << ProcessMemoryCounters.WorkingSetSize << std::endl << "QuotaPeakPagedPoolUsage: 0x" << std::setw(8) << ProcessMemoryCounters.QuotaPeakPagedPoolUsage << std::endl << "QuotaPagedPoolUsage: 0x" << std::setw(8) << ProcessMemoryCounters.QuotaPagedPoolUsage << std::endl << "QuotaPeakNonPagedPoolUsage: 0x" << std::setw(8) << ProcessMemoryCounters.QuotaPeakNonPagedPoolUsage << std::endl << "QuotaNonPagedPoolUsage: 0x" << std::setw(8) << ProcessMemoryCounters.QuotaNonPagedPoolUsage << std::endl << "PagefileUsage: 0x" << std::setw(8) << ProcessMemoryCounters.PagefileUsage << std::endl << "PeakPagefileUsage: 0x" << std::setw(8) << ProcessMemoryCounters.PeakPagefileUsage <<std::endl; } else std::cout << "Could not get memory usage (Error: " << ::GetLastError() << ")" << std::endl; // Close process ::CloseHandle(hProcess); } else std::cout << "Could not open process (Error " << ::GetLastError() << ")" << std::endl; return 0; }