先保存一下,以免以后用到又要重写
1 #include <windows.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <time.h>
5 #include "psapi.h"
6
7 DWORD GetMemory(int interval, int pid)
8 {
9 HANDLE hProcess = NULL;
10 PROCESS_MEMORY_COUNTERS pmc;
11 PERFORMACE_INFORMATION pmi;
12
13 hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
14 if (NULL == hProcess)
15 {
16 fprintf(stderr, "pid: %d not exist.\n", pid);
17 return -1;
18 }
19
20 setvbuf(stdout, NULL, _IONBF, 0);
21 while(1)
22 {
23 time_t t = time(NULL);
24 struct tm* nowtime = localtime(&t);
25 #if 1
26 if ( GetProcessMemoryInfo( hProcess, &pmc, (BYTE)sizeof(pmc)) )
27 #else
28 if (GetPerformanceInfo( hProcess, &pmi, (BYTE)sizeof(pmi)));
29 #endif
30 {
31 printf("%02d:%02d:%02d,%d\n", nowtime->tm_hour, nowtime->tm_min, nowtime->tm_sec,pmc.WorkingSetSize>>10);
32 }
33 Sleep(interval*1000);
34 }
35
36 CloseHandle( hProcess );
37 return 0;
38 }
39
40
41 int main(int argc, char* argv[])
42 {
43 int interval, pid;
44 if(argc != 3)
45 {
46 fprintf(stderr, "useage: interval pid\n", argv[0]);
47 exit(-1);
48 }
49 interval = atoi(argv[1]);
50 pid = atoi(argv[2]);
51
52 return GetMemory(interval, pid);
53 }
4 #include <time.h>
5 #include "psapi.h"
6
7 DWORD GetMemory(int interval, int pid)
8 {
9 HANDLE hProcess = NULL;
10 PROCESS_MEMORY_COUNTERS pmc;
11 PERFORMACE_INFORMATION pmi;
12
13 hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
14 if (NULL == hProcess)
15 {
16 fprintf(stderr, "pid: %d not exist.\n", pid);
17 return -1;
18 }
19
20 setvbuf(stdout, NULL, _IONBF, 0);
21 while(1)
22 {
23 time_t t = time(NULL);
24 struct tm* nowtime = localtime(&t);
25 #if 1
26 if ( GetProcessMemoryInfo( hProcess, &pmc, (BYTE)sizeof(pmc)) )
27 #else
28 if (GetPerformanceInfo( hProcess, &pmi, (BYTE)sizeof(pmi)));
29 #endif
30 {
31 printf("%02d:%02d:%02d,%d\n", nowtime->tm_hour, nowtime->tm_min, nowtime->tm_sec,pmc.WorkingSetSize>>10);
32 }
33 Sleep(interval*1000);
34 }
35
36 CloseHandle( hProcess );
37 return 0;
38 }
39
40
41 int main(int argc, char* argv[])
42 {
43 int interval, pid;
44 if(argc != 3)
45 {
46 fprintf(stderr, "useage: interval pid\n", argv[0]);
47 exit(-1);
48 }
49 interval = atoi(argv[1]);
50 pid = atoi(argv[2]);
51
52 return GetMemory(interval, pid);
53 }
浙公网安备 33010602011771号