Scott Lurndal <sc...@slp53.sl.home> wrote:
> Why do you say that? If I allocate all 1000 objects immediately
> and store their pointers, how is that fragmenting anything? Or
> if I allocate all 1000 objects in a single allocation and
> divide the allocation up?
If all your program ever allocates is those 1000 objects, then maybe.
However, programs do a lot more than that.
> And, for that matter, who cares if virtual memory is "fragmented"?
> (Physical memory is, of course, always "fragmented" into pages).
Traversing RAM in a random order is slower than traversing it
consecutively (regardless of whether it's physical or virtual
memory). When traversing it consecutively, the hardware prefethces
data and other such things.
>>Memory fragmentation increses overall memory consumption and slows down
>>speed (because it increases the amount of cache misses).
>
> 1) How does it increase overall memory consumption? Are you
> referring to the 8-16 byte overhead per allocation? That's
> in the noise.
Because memory fragmentation causes many gaps of free memory to
form between allocated blocks. When these gaps are too small for
a new allocation, additional memory will be allocated even though
there may be a lot of it free. It's just that all that free memory
is in such small gaps that the new allocation may not fit in any of
them.
There's a reason why many garbage-collected runtime systems (such
as the Java runtime) perform memory compaction. (Memory compaction
is rearranging all the allocated blocks into consecutive memory,
removing all the free gaps.)
> 2) How does it 'increase the amount (number) of cache misses',
> particularly when each object consumes multiple cache lines?
Because traversing memory in random order causes more cache misses
than traversing it consecutively.