"Private Bytes"没有反映它。如何找到进程分配的确切内存?
"Private Bytes" doesn't reflect it. How to find exact memory allocated by process?
下面是使用VS2010在Windows XP上编译和运行的c++代码示例。
在分配前后打印"private bytes"
void PrintPrivateBytes()
{
HANDLE current_process;
PROCESS_MEMORY_COUNTERS_EX pmc;
current_process = GetCurrentProcess();
if (!GetProcessMemoryInfo(current_process, (PPROCESS_MEMORY_COUNTERS)&pmc, sizeof(pmc)))
{
std::cout << "nGetProcessMemoryInfo failed" ;
return;
}
std::cout << "nProcess private bytes: " << pmc.PrivateUsage/1024 << " KB";
}
int _tmain(int argc, _TCHAR* argv[])
{
// Code demonstrating private bytes doesn't change
std::cout << "nnBefore allocating memory" ;
PrintPrivateBytes();
char* charptr = new char[8192];
std::cout << "nnAfter allocating 8 KB memory" ;
PrintPrivateBytes();
delete[] charptr;
std::cout << "nnAfter deleting memory" ;
PrintPrivateBytes();
int RetVal = _heapmin();
std::cout << "nnAfter calling _heapmin" ;
PrintPrivateBytes();
return 0;
}
输出:
分配内存前
进程私有字节:416 KB
分配内存后
进程私有字节:428 KB
删除内存后
进程私有字节:428 KB
调用_heapmin后
进程私有字节:428 KB
表示"私有字节"不能准确反映进程的内存使用情况。
哪个Windows API/结构将有助于找到确切的进程的内存使用情况?(工作组也没用。它只是反映了物理内存的使用情况)
您检查私有字节的解决方案是正确的,只有您对_heapmin的假设是错误的。
_heapmin不能正常工作。_heapmin在文档中描述为"向操作系统释放未使用的堆内存"。
实现(见"Program Files (x86)Microsoft Visual Studio 10.0VCcrtsrcheapmin.c")是
int __cdecl _heapmin(void)
{
if ( HeapCompact( _crtheap, 0 ) == 0 ) {
return -1;
}
else {
return 0;
}
}
在文档中,尽管返回堆中最大空闲块的大小,但HeapCompact通常什么也不做。如果使用了一个特殊的全局(调试目的)标志,它只会做一些额外的事情。
相关文章:
- 将数组的地址分配给变量并删除
- boost::进程间消息队列引发错误
- vector.resize()中的分配错误
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- Win32编译器选项和内存分配
- Win32 进程间通信分配
- 提升进程间:在循环中分配共享内存
- 将特定的逻辑内核专门分配给特定进程,即 Windows、C++
- 使用分配给其他进程的内存
- MPI_Scatter:为什么必须在所有进程中分配内存
- 如果在进程崩溃后在进程中分配内存,会发生什么情况
- 当内存被分配到Windows中的进程中时,它总是会触发页面错误,这是真的吗
- 如何制作进程以及它们执行不同的分配任务,例如使用fork读取文件或写入文件
- 当给出进程的终止命令时,如何处理分配的内存
- 将进程分配给处理器组
- "Private Bytes"没有反映它。如何找到进程分配的确切内存?
- 奇怪的 malloc 行为不允许在 64 位进程上分配超过 2GB 的内存
- CreateRemoteThread失败,可能目标进程中的lpBaseAddress无效,但系统分配了
- 在多个主机之间分配进程时,打开MPI程序不工作
- 为进程分配核心的好处