简体   繁体   English

“私人字节”没有反映出来。 如何找到进程分配的确切内存?

[英]“Private Bytes” doesn't reflect it. How to find exact memory allocated by process?

Here is sample piece of C++ code compiled and run using VS2010 on Windows XP. 这是在Windows XP上使用VS2010编译并运行的C ++代码示例。

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;
}

Here is output: 输出如下:

Before allocating memory 分配内存之前

Process private bytes: 416 KB 处理专用字节:416 KB

After allocating memory 分配内存后

Process private bytes: 428 KB 处理专用字节:428 KB

After deleting memory 删除记忆后

Process private bytes: 428 KB 处理专用字节:428 KB

After calling _heapmin 调用_heapmin之后

Process private bytes: 428 KB 处理专用字节:428 KB

It indicates "private bytes" doesn't reflect exact memory usage of process. 它指示“专用字节”不反映进程的确切内存使用情况。

Which Windows API/structure will help finding exact memory usage of process ? 哪种Windows API /结构将有助于查找进程的确切内存使用情况? (Working set is also of no use. It just reflect much how physical memory is being used) (工作集也没有用。它只是反映了物理内存的使用情况)

Your solution of checking the private bytes is correct, only your assumption about _heapmin is wrong. 您检查专用字节的解决方案是正确的,只有关于_heapmin的假设是错误的。

_heapmin does not work as documented. _heapmin不能按说明工作。 _heapmin is documented as "Releases unused heap memory to the operating system." _heapmin被记录为“将未使用的堆内存释放给操作系统”。

The implementation (see "\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\crt\\src\\heapmin.c") is 实现(请参见“ \\ Program Files(x86)\\ Microsoft Visual Studio 10.0 \\ VC \\ crt \\ src \\ heapmin.c”)

int __cdecl _heapmin(void)
{
        if ( HeapCompact( _crtheap, 0 ) == 0 ) {
            return -1;
        }
        else {
            return 0;
        }
}

HeapCompact is documented to normally do quite nothing despite returning the size of the largest free block in the heap. 尽管返回了堆中最大的空闲块的大小,但是HeapCompact被记录为通常不执行任何操作。 It only does some extra stuff if a special global (debug purpose) flag is used. 如果使用特殊的全局(调试目的)标志,它只会做一些额外的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM