heapcreate/heapfree and reserved segments

Tech-Archive recommends: Speed Up your PC by fixing your registry



Symptoms:

Memory allocations using HeapAlloc fail even when there are large amounts of
uncommitted memory available within the address space of the process.

Our Win32 applications encounter unexpected memory allocation failures at
certain sites when process reserved virtual address space approaches the 2GB
limit. When we analyze heap utilisation in process dumps from the site we
find significant amounts of unused free space in the form of free blocks
and large regions of contiguous uncommitted pages.

Background:

This application employs aggressive memory caching strategies for different
purposes and primarily uses the HeapCreate (and associated allocation
functions) to create and manage multiple heaps. Multiple heaps are created
for various reasons, primarily to achieve isolation at a module level. Some
heaps are created on demand only when a particular module is invoked.
VirtualAlloc and VirtualFree are also used within the product.

Description:

1) Observed Heap growth behaviour

When a Heap is created using HeapCreate (and zero initial and maximum size),
an initial 256KB segment is reserved. When the heap is first extended, the
next segment is 1MB and successive extensions double the size of the
previous segment until the segment size reaches a 256MB limit.

2) Observed segment retention behaviour

When the application frees blocks using HeapFree and there are segments in
the heap containing no blocks allocated by the application, reserved
segments are not freed. If you allocate blocks in a heap and grow it to 1GB
say, then free all allocated blocks the reserved virtual address space
remains at 1GB.

Each of these behavioural characteristics gives rise to excessive address
space fragmentation with a low effective usage of process virtual memory.
For example approximately 256MB of total bytes allocated in a heap can
result in 512MB virtual address space utilisation with a sparsely filled
final segment.

This poor Virtual Memory usage efficiency phenomenon makes it very difficult
for our users to configure various cache sizes and per user memory
allocations since the amount of VM address space overhead that will be
incurred in complex and varying usage contexts is too difficult to predict
with any degree of accuracy.

Questions:

1.. Is there any way for an application to influence or override the
described behaviour?
In particular, either: Control the size of segments (heap extent size) or
request that unused segments (address space) to be returned. An improved
HeapCompact perhaps??
2.. Is there any other way to mitigate this issue apart from not using
multiple heaps?
3.. Initial investigation using the HeapCompact API has shown this has no
discernable effect on heap structure? Under what conditions is this
effective ?


.



Relevant Pages

  • Memory problems - WinDbg and SOS: Who recognizes this pattern?
    ... Last night I managed to get a memory dump using ADPlus and I analyzed it ... ephemeral segment allocation context: none ... Large object heap starts at 0x0a0d0030 ...
    (microsoft.public.dotnet.framework.performance)
  • Re: Questions about Minix
    ... I think I got to that conclusion that .text shared the same memory chunk ... segment to processes from the beginning, and a data segment that did not ... True, but this would limit your stack size to some predetermined value, ... you still have plenty of heap left. ...
    (comp.os.minix)
  • Re: Memory is fragmented even though everything is deallocated
    ... I am running Windows XP indeed. ... Plus, if I destroy the process heap, will I be able to allocate on the ... My guess is that deallocated memory is not released by the ... for small allocations only, since the huge chunk of memory allocated ...
    (microsoft.public.vc.language)
  • Re: heapcreate/heapfree and reserved segments
    ... Isolation is more per functional module in a multi-client/server situation. ... > You can try increasing the initial heap size. ... >> Memory allocations using HeapAlloc fail even when there are large ... >> an initial 256KB segment is reserved. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: ptrs validity
    ... I have a pointer that points to an unknown heap memory block, ... hardware checked segment for each allocation. ...
    (comp.lang.c)