locked
Windows 7 Heap behavior and LFH RRS feed

  • Question

  • According to MSDN documentation and all the posts the LFH is enabled by default in Windows 7.I made some test program based on MS example:

    int _tmain(int argc, _TCHAR* argv[])
    {
    	ULONG HeapFragValue = 2;
    	ULONG heap_info = 0;
    	SIZE_T ret_size = 0;
    
    	HANDLE* phHeaps = NULL;
    	DWORD dwHeaps = GetProcessHeaps(0,phHeaps);
    	printf("Heaps found: %u\n", dwHeaps);
    	const HANDLE default_heap = ::GetProcessHeap ();
    	const HANDLE crt_heap = (HANDLE) _get_heap_handle ();	phHeaps = new HANDLE[dwHeaps+1];
    	::ZeroMemory(phHeaps,sizeof(HANDLE)*(dwHeaps+1));
    	dwHeaps = GetProcessHeaps(dwHeaps,phHeaps);
    	for (int i =0; i < (int)dwHeaps; i++)
    	{
    		if (HeapQueryInformation (phHeaps[i],
    			HeapCompatibilityInformation,
    			&heap_info,
    			sizeof (heap_info),
    			&ret_size))
    		{
    			if ( phHeaps[i] == crt_heap )
    			{
    				printf("CRT ");
    			}
    			if ( phHeaps[i] == default_heap )
    			{
    				printf("Process default ");
    			}
    			switch (heap_info)
    			{
    
    			case 0:
    				printf ("Heap 0x%X is a regular heap.\n",phHeaps[i]);
    				break;
    			case 1:
    				printf ("Heap 0x%X is a heap with look-asides (fast heap).\n",phHeaps[i]);
    				break;
    			case 2:
    				printf ("Heap 0x%X is a LFH (low-fragmentation) heap.\n",phHeaps[i]);
    				break;
    			default:
    				printf ("Heap 0x%X is of unknown type.\n",phHeaps[i]);
    				break;
    			}
    		}
    		else
    		{
    			printf ("Error querruing heap 0x%X .\n",phHeaps[i]);
    		}
    	}
    	return 0;
    }

    when I run it on Windows 7 I get output like this:

    Heaps found: 4
    Process default Heap 0x370000 is a LFH (low-fragmentation) heap.
    Heap 0x10000 is a regular heap.
    Heap 0x20000 is a regular heap.
    CRT Heap 0x590000 is a LFH (low-fragmentation) heap.

    As you can see the current process has 4 heaps , two of them (default and CRT) are LFH indeed, but two others are just regular heaps.

    Someone care to explain me what are they, why and why they are not LFH (they are not look-asides either, and I am not running under debug)?

    From what I understand they are heaps from some DLLs? However project was compiled wth no ATL or MFC...

    The bigger question here is actually how smart the Windows 7 decisions?

    Let me explain, we have a program on Windows XP that does a tons of small allocations, enabling LFH "blindly" there (just go over all heaps and Set them as LFH) did a huge performance increase.

    Now we (ported) moved to Windows 7 x64 and since all the docs state Win7 has LFH enabled by default we wanted to remove this code. However after some testing we noticed that if we use this code then out of 52 heaps we have 38 are become LFH, while if we use the defaults Win7 (without forcing LFH by our code) then only 28 heaps (out of same 52) are LFH.

    Of course we will do some tests to compare, but I wish to understand the logic behind it and why this does not match MS explanation of LFH being enabled by default.


    Wednesday, July 17, 2013 8:33 AM