Board index » delphi » New() vs. Global Alloc

New() vs. Global Alloc

From "Object Pascal Language Guide" --

"...Each global memory block carries an overhead of at least 20 bytes,
and there is a system-wide limit of 8192 global memory blocks, only
some of which are available to any given application."  Pg 161, "The
heap manager", par 2, Chp 16.

"Delphi includes a heap manager which implements the New, Dispose,
GetMem, and FreeMem standard procedures.  The heap manager uses the
global heap for all allocations.  Because the global heap has  a
system-wide limit of 8192 memory blocks ... Delphi's heap manager
includes a segment sub-allocator algorithm to enhance performance and
allow a substantially larger number of blocks to be allocated."  Pg
161, "The heap manager", par 4, Chp 16.

then it states:

"The HeapLimit variable defines the threshold between small and large
heap blocks.  The default value is 1024 bytes.  The HeapBlock variable
defines the size of the heap manager uses when allocating blocks to be
assigned to the sub-allocator.  The default value of HeapBlock is 8192
bytes.",  Pg 162, "The heap manager", par 6, Chp 16.

What this is telling me, is that after every 8 allocations of 1K each,
it allocates a new chunck of memory.  Therefore, after 8,192
allocations of 8K you would reach the limit of Windows.

The problem that I am having is that I get a GPF after 29 allocations
of 280 bytes (8,120 bytes total).  At this point, it should allocate
another 8K and use its second global memory block.  I understand that
other parts of the application could be using their own global blocks
(for classes, etc..), however, this application is not that
complicated, nor does it allocate a lot of global memory  (form has 2
buttons, and a memo) except for the Binary tree (which is where its
blowing up on the New()).

If I change the size of the allocations from 280 bytes to something
less, then the number of allocations changes in relation to the change
in the size of the allocation.  As soon as the sum of the allocations
reaches 8K it GPF's.

The question is, is this:  Will changing the HeapLimit or HeapBlock
get rid of this problem, and which would have a more significant
effect.

Any ideas on this would be greatly appreciated.

Brien King
bk...@primenet.com

 

Re:New() vs. Global Alloc


        It seems you must be doing something wrong somewhere; can't say what
the problem is without seeing any code, but I don't have any difficulties
like this. I do

type
  PBytes = ^TBytes;
  TBytes = array[0..1000] of byte;

procedure TForm1.Button1Click(Sender: TObject);
var LotsABytes:array[0..1000] of PBytes;j:integer;
begin
    for j:=0 to 1000 do new(LotsABytes[j]);
    for j:=0 to 1000 do dispose(LotsABytes[j]);
end;

and there's no problem.

--
David Ullrich
Don't you guys find it tedious typing the same thing
after your signature each time you post something?
I know I do, but when in Rome...

Re:New() vs. Global Alloc


Quote
David Ullrich <ullr...@math.okstate.edu> wrote:
>    It seems you must be doing something wrong somewhere; can't say what
>the problem is without seeing any code, but I don't have any difficulties
>like this. I do
>type
>  PBytes = ^TBytes;
>  TBytes = array[0..1000] of byte;
>procedure TForm1.Button1Click(Sender: TObject);
>var LotsABytes:array[0..1000] of PBytes;j:integer;
>begin
>    for j:=0 to 1000 do new(LotsABytes[j]);
>    for j:=0 to 1000 do dispose(LotsABytes[j]);
>end;
>and there's no problem.

I'd show some code, but which of the 63,000+ lines do you want to see?
<G>.   The code in question is working when I adjust the HeapLimit and
HeapBlock values, otherwise I get a GPF when I try to do a New,
GetMem, or AllocMem when I reach that magic number.  

I have a hard time believing that the Heap Manager is working
correctly.  Either that or I don't understand it correctly (which
would not be unusual).

If I lower the HeapLimit, then I can allocate more memory.  If I raise
HeapLimit, then I am limited to what ever the HeapBlock is set to (or
8k, haven't tested this as much as I should, but deadlines are
approaching, 1 Week left.. AAAAAAAAAHHHH).

If I have HeapLimit = 1024 and HeapBlock = 8192 then I can only
allocate 8K.  However, if I have HeapLimit = 128 and HeapBlock=32768
then I can allocate over 500K...   If I set HeapLimit to 0, I get a
GPF when the application terminates.  Change HeapLimit to 128 and it
works just fine.  Go figure.

If someone from Borland (who is lurking here) could explain in detail
how the Heap Manager works, I would be greatful.  None of the books
explain this very well (Mastering Delphi has the best info on memory
issues but still leaves a lot to be desired.).

Brien King
bk...@primenet.com

Re:New() vs. Global Alloc


Quote
>I'd show some code, but which of the 63,000+ lines do you want to see?
><G>.   The code in question is working when I adjust the HeapLimit and
>HeapBlock values, otherwise I get a GPF when I try to do a New,
>GetMem, or AllocMem when I reach that magic number.

>I have a hard time believing that the Heap Manager is working
>correctly.  Either that or I don't understand it correctly (which
>would not be unusual).

        Well whatever. Nobody else seems to have problems with the heap
manager like this. Don't they say something about how you can adjust
HeapLimit but they don't think you'll ever need to?
        If you take an example like the one I posted (the point being I
know that that example works for me) does it work for you with the default
settings?

--
David Ullrich
Don't you guys find it tedious typing the same thing
after your signature each time you post something?
I know I do, but when in Rome...

Re:New() vs. Global Alloc


On 29 Jan 1996 16:27:01 -0700, bk...@primenet.com (Brien King) wrote:

Quote
>The problem that I am having is that I get a GPF after 29 allocations
>of 280 bytes (8,120 bytes total).  At this point, it should allocate
>another 8K and use its second global memory block.  I understand that
>other parts of the application could be using their own global blocks
>(for classes, etc..), however, this application is not that
>complicated, nor does it allocate a lot of global memory  (form has 2
>buttons, and a memo) except for the Binary tree (which is where its
>blowing up on the New()).
...
>The question is, is this:  Will changing the HeapLimit or HeapBlock
>get rid of this problem, and which would have a more significant
>effect.

I doubt the problem is caused by Delphi's suballocator. Most likely,
there is a memory overrun, freeing memory twice, etc.

One trick you can try is to set HeapLimit to zero. This forces every
allocation to go to Windows, bypassing the suballocator completely.
When freeing memory, the segment is freed and becomes invalid. An
attempt to free memory twice raises an exception immediately, which
makes it easier to debug.
--
Ray Lischner         (li...@tempest-sw.com)
Tempest Software, Corvallis, Oregon, USA

Other Threads