c - 在windows下查找程序在c中使用的总内存

标签 c memory

我有一个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.WorkingSetSizememory.PrivateUsage,无论哪个度量对您来说都很重要。这些是 size_t 类型,它们是大小取决于您的系统的无符号整数。

关于c - 在windows下查找程序在c中使用的总内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8935352/

相关文章:

c - TIBCO-RV 数据包的 header 和消息正文的格式是什么?

c# - 在循环中创建位图时出现内存溢出异常

根据位置从链表创建子列表?

c - 尝试从 C 编程语言完成 E1-19 时出现段错误

c - 错误: Incompatible integer to pointer conversion passing 'unsigned long'

memory - PHP 警告 : POST Content-Length of 113 bytes exceeds the limit of -1988100096 bytes in Unknown

ios - NSData的内容是单独引用计数的吗?

c - linux 内核 - pte_xxx() API 在模块编程中不起作用

c++ - boost::container::small_vector 似乎没有就地分配

python - 将 Python 对象转换为 C void 类型