Thread: Getting heap allocations

  1. #1
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396

    Getting heap allocations

    I'm using GetProcessHeaps() and HeapWalk() to step through all the heaps in a program, counting the total allocations.

    Strangely, the number of allocated bytes doesn't seem to change, even when I use new to allocate a huge data structure -- several tens of megabytes.

    Is the MSVC operator new getting memory from some other location other than the heap? I'm digging around MSDN but haven't found any info yet.

    I want to find a way to track allocations on the byte (not page) level, and I really don't want to go down the path of overriding global new/delete.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  2. #2
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Depends how you're counting the bytes. If you're just counting the dwCommittedSize of the HEAP_REGION blocks you'll miss the big blocks since they are counted as seperate regions but not tagged as such (it's described in the PROCESS_HEAP_ENTRY::iRegionIndex docs). Summing up the cbData member of all PROCESS_HEAP_ENTRY_BUSY blocks will get you an accurate committed byte count.

    Maybe this'll help:
    Code:
    void DisplayHeapsInfo(std::ostream& out = std::cout)
    {
        std::vector<HANDLE> heaps(GetProcessHeaps(0, NULL));
        GetProcessHeaps(heaps.size(), &heaps[0]);
        DWORD totalBytes = 0;
        for(DWORD i = 0; i < heaps.size(); ++i)
        {
            out << "Heap handle: 0x" << heaps[i] << '\n';
            PROCESS_HEAP_ENTRY phi = {0};
            while(HeapWalk(heaps[i], &phi))
            {
                out << "Block Start Address: 0x" << phi.lpData << '\n';
                out << "\tSize: " << phi.cbData << " - Overhead: "
                               << static_cast<DWORD>(phi.cbOverhead) << '\n';
                out << "\tBlock is a";
                if(phi.wFlags & PROCESS_HEAP_REGION)
                {
                    out << " VMem region:\n";
                    out << "\tCommitted size: " << phi.Region.dwCommittedSize << '\n';
                    out << "\tUncomitted size: " << phi.Region.dwUnCommittedSize << '\n';
                    out << "\tFirst block: 0x" << phi.Region.lpFirstBlock << '\n';
                    out << "\tLast block: 0x" << phi.Region.lpLastBlock << '\n';
                }
                else 
                {
                    if(phi.wFlags & PROCESS_HEAP_UNCOMMITTED_RANGE)
                    {
                        out << "n uncommitted range\n";
                    }
                    else if(phi.wFlags & PROCESS_HEAP_ENTRY_BUSY)
                    {
                        totalBytes += phi.cbData;
                        out << "n Allocated range: Region index - " 
                                       << static_cast<unsigned>(phi.iRegionIndex) << '\n';
                        if(phi.wFlags & PROCESS_HEAP_ENTRY_MOVEABLE)
                        {
                            out << "\tMovable: Handle is 0x" << phi.Block.hMem << '\n';
                        }
                        else if(phi.wFlags & PROCESS_HEAP_ENTRY_DDESHARE)
                        {
                            out << "\tDDE Sharable\n";
                        }
                    }
                    else out << " block, no other flags specified\n";
                }
                out << std::endl;
            }
        }
        out << "End of report - total of " << std::dec << totalBytes << " allocated" << std::endl;
    }

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Very good information. Thanks!
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. heap
    By George2 in forum Windows Programming
    Replies: 2
    Last Post: 11-10-2007, 11:49 PM
  2. Suggestions on this C style code
    By Joelito in forum C Programming
    Replies: 11
    Last Post: 06-07-2007, 03:22 AM
  3. Heap Work
    By AndyBomstad in forum C++ Programming
    Replies: 1
    Last Post: 05-16-2005, 12:09 PM
  4. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  5. heap question
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 05:03 AM