问题描述
这里是使用VS2010在Windows XP上编译和运行的C ++代码示例。
Here is sample piece of C++ code compiled and run using VS2010 on Windows XP.
在分配前后打印私有字节。
It prints "private bytes" before and after allocation.
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 << "\n\nBefore allocating memory" ;
PrintPrivateBytes();
char* charptr = new char[8192];
std::cout << "\n\nAfter allocating 8 KB memory" ;
PrintPrivateBytes();
delete[] charptr;
std::cout << "\n\nAfter deleting memory" ;
PrintPrivateBytes();
int RetVal = _heapmin();
std::cout << "\n\nAfter calling _heapmin" ;
PrintPrivateBytes();
return 0;
}
这里是输出:
处理私人字节:416 KB
Process private bytes: 416 KB
分配内存
处理专用字节:428 KB
Process private bytes: 428 KB
删除内存后
处理私人字节:428 KB
Process private bytes: 428 KB
调用_heapmin后
After calling _heapmin
处理私人字节:428 KB
Process private bytes: 428 KB
它指示私有字节不反映进程的确切内存使用情况。
It indicates "private bytes" doesn't reflect exact memory usage of process.
哪些Windows API /结构有助于查找进程的确切内存使用情况?
(工作集也没有用,它只反映了物理内存的使用方式)
Which Windows API/structure will help finding exact memory usage of process ?(Working set is also of no use. It just reflect much how physical memory is being used)
推荐答案
检查私有字节的解决方案是正确的,只有你对_heapmin的假设是错误的。
Your solution of checking the private bytes is correct, only your assumption about _heapmin is wrong.
_heapmin不能正常工作。 _heapmin是为向操作系统释放未使用的堆内存。
_heapmin does not work as documented. _heapmin is documented as "Releases unused heap memory to the operating system."
实施(请参阅\Program文件(x86)\ Microsoft Visual Studio 10.0 \VC\crt\src\heapmin.c)
The implementation (see "\Program Files (x86)\Microsoft Visual Studio 10.0\VC\crt\src\heapmin.c") is
int __cdecl _heapmin(void)
{
if ( HeapCompact( _crtheap, 0 ) == 0 ) {
return -1;
}
else {
return 0;
}
}
HeapCompact 通常做什么,尽管返回的大小堆中最大的空闲块。如果使用特殊的全局(调试目的)标志,它只会做一些额外的东西。
HeapCompact is documented to normally do quite nothing despite returning the size of the largest free block in the heap. It only does some extra stuff if a special global (debug purpose) flag is used.
这篇关于“私有字节”没有反映它。如何找到由进程分配的确切内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!