Problem while setting Low Fragmentation Heap

154 views
Skip to first unread message

WDev

unread,
Dec 23, 2002, 2:26:09 PM12/23/02
to
I am trying to use the Low Fragmentation Heap on the default heap on a XP
platform.

The problem is that HeapSetInformation always return FALSE with a
GetLastError = 31 (ERROR_GEN_FAILURE)

the code is simple:

ULONG ulValue=2;

HANDLE hHeap=GetProcessHeap();

BOOL
bOk=::HeapSetInformation(hHeap,HeapCompatibilityInformation,&ulValue,sizeof(
ULONG));


Do you know what i am doing wrong?

Thanks in advance


Ivan Brugiolo [MSFT]

unread,
Dec 24, 2002, 11:20:41 AM12/24/02
to
Does it happen when you running the process under debugger or all the times
?

--
This posting is provided "As Is" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


"WDev" <tp...@hotmail.com> wrote in message
news:lrJN9.85774$qF3.7194@sccrnsc04...

WDev

unread,
Dec 24, 2002, 12:02:02 PM12/24/02
to
It seems extrange:

Yes, I test setting the low fragmentation on the default heap outside the
debugger (writing a trace file) and it works. But I also test setting low
fragmention on other heap created by myself (not the default process heap),
and it doesn't work, both within the debugger and outside.

Do you know why this happend?.

"Ivan Brugiolo [MSFT]" <ivan...@online.microsoft.com> escribió en el
mensaje news:efHihh2qCHA.2308@TK2MSFTNGP09...

Ivan Brugiolo [MSFT]

unread,
Dec 24, 2002, 12:11:16 PM12/24/02
to
The debugger enable certain flags on all the heaps, so that certain common
problems can be caught.
The LowFrag Heap is a piece of code really hard to debug when you have
corruptions and/or double
free and or reuser-after-delete, while the regular heap is somehow more
resistent to these problems.
(is you have a heap corruption on the regular heap there are 20%-30% chances
to find the culprit
by analysis on the stack and the heap, while with LowFrag the chances are
near zero,
that's why its not recomended for debug purposes).

Can I see the code you use to create the private Heaps ?
I wonder if you are putting some incompatible flags there.

--
This posting is provided "As Is" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


"WDev" <tp...@hotmail.com> wrote in message

news:eq0O9.292514$pN3.22689@sccrnsc03...

WDev

unread,
Dec 24, 2002, 12:35:56 PM12/24/02
to
The flag that I am using when creating the particular heap is
HEAP_NO_SERIALIZE. Do you know if that could cause the fail?, do you know
why?. Thanks for your answers.

Anyway I post you the code below. I create the Heap on a Class (CMemAlloc),
the first method create the heap (retrying with a inferior size if fail),
and the second simply set the Low fragmentation flag (note that both use the
common class data member m_hHeap as heap handle).

//------------------------------------------------------------------

void CMemAlloc::CreateHeap(int iReqSize, bool bThreadSafeMode )

{

DWORD dwFlags;

int iSize=iReqSize;

if( bThreadSafeMode )

dwFlags=0;

else

dwFlags=HEAP_NO_SERIALIZE;

do

{ // keep trying until no error or too litle

m_hHeap = ::HeapCreate( dwFlags, // heap flags (CMemAlloc would also
controls lock serialization)

iSize, // initial size of heap

0); // max heap size

iSize /= 2; // try a smaller request

if( iSize < (int)(m_BlockSize*2) )

{

GLogError4("Fail on create Heap of size %d (Intended %d, BlockSize %d,
bThreadSafeMode %d), using default
heap",iSize*2,iReqSize,m_BlockSize,(int)bThreadSafeMode);

return; // heap would be too small to be usefull, return

// if m_hHeap is ok and even exits here is no problem.

}

}

while( m_hHeap == NULL );

GLogTrace4("Heap created of size %d (Intended %d, BlockSize %d,
bThreadSafeMode %d)",iSize*2,iReqSize,m_BlockSize,(int)bThreadSafeMode);

}

//------------------------------------------------------------------

bool CMemAlloc::SetLowFragmentation()

{

ULONG ulValue=2;

HANDLE hHeap=m_hHeap;

if( hHeap==NULL )

hHeap=GetProcessHeap();

return
::HeapSetInformation(hHeap,HeapCompatibilityInformation,&ulValue,sizeof(ULON
G))==TRUE;

}

"Ivan Brugiolo [MSFT]" <ivan...@online.microsoft.com> escribió en el

mensaje news:esfOy92qCHA.1964@TK2MSFTNGP09...

Ivan Brugiolo [MSFT]

unread,
Dec 24, 2002, 1:10:12 PM12/24/02
to
If you are choosing no serialization, then you don't need the LowFragHeap.
So, your actual problem is that flag.

On the bigger picture, the LowFragHeap is a heap has features such as:
-o- Thread affine allocation in the average case (this guarantees cosistency
with virtual no cost for lock)
-o- Low Fragmentation and almost constant working set for applications with
an istogram
of allocations centered in the low-size part of the graph, and well
contained in the low-size part of the graph.

If you are claiming you known how to lock you heap,
then you are possibly destroying the LowfragHeap heap algorithms,
hence there is no point using them.
Or, in different terms, if you don't need the lock services from the heap,
it means that your application is single threaded anyway,
or you have a per-thread heap with no cross
heap contaminations problem in your code.

Please reflect on the usage of the private heaps, the locking strategy of
the allocator,
the memory graph usage runtime and the frequency of block reuse
before throwing in the LowFrag heap.

--
This posting is provided "As Is" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


"WDev" <tp...@hotmail.com> wrote in message

news:0W0O9.492640$QZ.74249@sccrnsc02...

WDev

unread,
Dec 24, 2002, 3:09:35 PM12/24/02
to
Well, that the case: I have a per-thread exclusive heap, that is because a
use the HEAP_NO_SERIALIZE on it (there isn't needed any locking).
Additionaly that heap it used normally for smalls allocations (< 16k), with
a high reuse rate(frecuent "frees" and new allocations), so that is the
reason because I want to use the Low Fragmentation Heap. I still think it
would be usefull for that (based on your second named feature).

Thanks for the information anyway. Do you know where I could find extended
information about the Low Frag Heap?, since I didn't find in the
documentation information like you just had given to me.

"Ivan Brugiolo [MSFT]" <ivan...@online.microsoft.com> escribió en el

mensaje news:eERSue3qCHA.2304@TK2MSFTNGP12...

Reply all
Reply to author
Forward
0 new messages