我有一个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;
};
我想知道这个程序使用了多少内存。我应该添加任何代码或函数来查找该程序使用的总内存。我不想为此检查任务管理器。我需要打印内存使用情况。
最佳答案
听起来你在寻找 GetProcessMemoryInfo()
.
您需要 Windows SDK 或来自 mingw32 的相应 Windows header 文件才能编译它(也可以在 Visual Studio 中使用,以及带有适当库的 cygwin 等):
#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
类型,它们是大小取决于您的系统的无符号整数。
关于c - 在windows下查找程序在c中使用的总内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8935352/