Re: Heap fragmentation



Thomas Ruschival <Thomas@xxxxxxxxxxxx> wrote:
Hi groups,
in University I learnt about heap fragmentation, and I wanted to measure
it by some example code written in C running linux. Strangely I
couldn't measure any timing differences.
What I do is allocate 500 000 chunks of memory all randomly sized between
1 and 1234 bytes and place them in an array of fixed size. This action is
measured. Then I free each element of the array again.
over this procedure I cycle a hundred times.
After the first cycle I expect to have a horribly fragmented heap but
_all_ cycles take the same time to execute plus minus 4 milliseconds
which I think come form scheduling.
I there any wrong in my assumption or measurement, or is there a fancy
low fragmentation heap management in Linux glibc?

You're allocating everything at once and then deallocating everything at
once. Chances are good that the same memory us used each time. If you
have (for example) a megabyte of allocation, it's likely to be in
contiguous (virtual) memory in the absence of any other activity. You then
free all of that, and chances are that the next time you allocate you're
going to reuse the nice big contiguous memory area you're just freed up.

You should allocate and deallocate randomly as well. Try something like

for (lot os times)
- allocate a random chunk at a random index into your array
- free a chunk at another random location in your array if it's
been allocated

That way you're randomising the allocation and deallocation order
of chunks of memory, and your heap will quickly fragment.

Hmmm, having said that it'll probably eventually settle down to some
pattern in memory that's based on the maximum or average size of your
allocated chunks.

--
Nobby Anderson
.



Relevant Pages

  • Re: Struct inside class
    ... The compiler figures computes the ... including anything derived from "Object": Heap pointer ... memory leaks and why the .NET good garbage collector is required. ... If GC_ALLOCATE can't allocate the requested ...
    (microsoft.public.dotnet.framework)
  • Re: run-time vs compile-time
    ... > offset related to some location (like stack base) somewhere. ... > offset from heap to pi. ... When you allocate an int on the heap, it is allocated at address 1. ... application has a given amount of memory it can use as it wishes. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: run-time vs compile-time
    ... > offset related to some location (like stack base) somewhere. ... > offset from heap to pi. ... When you allocate an int on the heap, it is allocated at address 1. ... application has a given amount of memory it can use as it wishes. ...
    (comp.lang.cpp)
  • Re: Huge pages and small pages. . .
    ... >> what looks like contiguous memory and away you go. ... you need to have them cached in the TLB; if the TLB runs out of ... > low end of the heap, until someone figures out a way to tell the system ... When you allocate memory, the kernel just marks a promised ...
    (Linux-Kernel)
  • Re: Memory problems (possibly very easy question)
    ... Now allocate 25,000 objects. ... Once you get the leaks fixed, fragmentation becomes your worst enemy. ... memory usage warnings. ... But releasing it does not reduce the footprint, it merely keeps it from growing as the ...
    (microsoft.public.vc.mfc)