Created
January 2, 2015 06:30
-
-
Save duguying/5af11f7fc6e8e16fe251 to your computer and use it in GitHub Desktop.
获取windows子进程的内存使用情况
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//ProcessMemory.cpp | |
//用法: | |
// argv[1]是子进程的包含路径的完整可执行文件名 | |
// ProcessMemory notepad | |
//编译: | |
// cl ProcessMemory.cpp | |
//功能: | |
// 获取子进程的内存使用情况 | |
#include <stdio.h> | |
#include <windows.h> | |
#include <psapi.h> | |
#pragma comment(lib, "psapi.lib") | |
int main(int argc, char *argv[]) | |
{ | |
//CreateProcess的第一个参数 | |
STARTUPINFO StartupInfo = {0}; | |
StartupInfo.cb = sizeof(STARTUPINFO); | |
//CreateProcess的第二个参数 | |
PROCESS_INFORMATION ProcessInfo = {0}; | |
CreateProcess(NULL, argv[1], NULL, NULL, TRUE, NULL, NULL, NULL, &StartupInfo, &ProcessInfo); | |
//作用为:子进程运行结束后,查看它的内存使用情况。 | |
// 此时,所有非Peak的项均为0,所有Peak项显示运行期间的峰值 | |
//如果想在子进程结束之前监控它的内存使用情况,可以将这句替换为 | |
// Sleep(1000); | |
// 作用为:子进程运行1000毫秒之后,查看它的内存使用情况 | |
WaitForSingleObject(ProcessInfo.hProcess, INFINITE); | |
PROCESS_MEMORY_COUNTERS pmc; | |
GetProcessMemoryInfo(ProcessInfo.hProcess, &pmc, sizeof(pmc)); | |
//以下各项均以KB为单位 | |
//在任务管理器中显示为:峰值工作设置(内存) | |
printf("%d\n", pmc.PeakWorkingSetSize/1024); | |
//在任务管理器中显示为:工作设置(内存) | |
printf("%d\n", pmc.WorkingSetSize/1024); | |
printf("%d\n", pmc.QuotaPeakPagedPoolUsage/1024); | |
//在任务管理器中显示为:分页池 | |
printf("%d\n", pmc.QuotaPagedPoolUsage/1024); | |
printf("%d\n", pmc.QuotaPeakNonPagedPoolUsage/1024); | |
//在任务管理器中显示为:非页面缓冲池 | |
printf("%d\n", pmc.QuotaNonPagedPoolUsage/1024); | |
printf("%d\n", pmc.PeakPagefileUsage/1024); | |
//在任务管理器中显示为:内存(专用工作集) //这是任务管理器的默认显示项! | |
printf("%d\n", pmc.PagefileUsage/1024); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment