找到windows下用C使用的程序的总内存 [英] find total memory used by program in c under windows
问题描述
我在C小程序
#include <stdio.h>
#include <string.h>
#define SIZE 30
int main()
{
char name[ SIZE ];
FILE *fpPtr;
if ( ( fpPtr = fopen( "sorted_file.txt", "r" ) ) == NULL ) {
printf( "File could not be opened\n" );
}
else {
printf( "%s\n", "Name" );
fscanf( fpPtr, "%s", name );
while( !feof( fpPtr ) ) {
printf( "%s\n", name );
fscanf( fpPtr, "%s", name );
}
fclose( fpPtr );
}
return 0;
};
我要找出多少内存是这个程序中使用。任何code或功能我要补充找到该程序使用的内存总量。我不想检查任务管理器这一点。我需要打印内存使用情况。
I want to find out how much memory is this program using . Any code or function i should add to find the total memory used by this program. I don't want to check task manager for this. I need to print the memory usage.
推荐答案
这听起来像你后的 GetProcessMemoryInfo()
。
It sounds like you're after GetProcessMemoryInfo()
.
您需要在Windows SDK或相应的窗口从mingw32的头文件,以便编译这个(也将在Visual Studio中工作,太,以及cygwin的等适当的库):
You'll need the Windows SDK or the appropriate windows headers from mingw32 in order to compile this (will also work in Visual Studio, too, as well as cygwin etc with appropriate libraries):
#include <windows.h> // these header gives you access to the Windows API -
#include <psapi.h> // include at the top of your code
// do this where you need to get that information - perhaps create a function
// for it.
HINSTANCE hProcHandle = GetModuleHandle(NULL); // get the current process handle
PROCESS_MEMORY_COUNTERS_EX memory; // output will go here.
/* call function */
GetProcessMemoryInfo(hProcHandle, &memory, sizeof(memory));
您现在应该能够访问 memory.WorkingSetSize
或 memory.PrivateUsage
,任何措施的问题给你。这些是为size_t
类型这是大小的无符号整数取决于你的系统。
You should now be able to access memory.WorkingSetSize
or memory.PrivateUsage
, whichever measure matters to you. These are size_t
types which are unsigned integers of size dependent on your system.
这篇关于找到windows下用C使用的程序的总内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!