In article <>,
says...
> On Thu, 10 Apr 2008 17:33:21 +0300, Juha Nieminen
> <> wrote:
>
> > However, part of my C++ programming style just naturally also avoids
> >doing tons of news and deletes in tight loops (which is, again, very
> >different from eg. Java programming where you basically have no choice)
>
> Howeever, Java allocates new memory blocks on it's internal heap
> (which is allocated in huge chunks from the OS). In this way, in most
> of the cases it bypasses memory allocation mechanisms of the
> underlying OS and is very fast. In C++, each "new" allocation request
> will be sent to the operating system, which is slow.
I know of one library implementation for which this was true (one of the
first 32-bit versions of Visual C++), but it's generally false. Even in
that version of that compiler, the library _contained_ code to allocate
from the OS in large blocks, and then do sub-allocations out of that
block. For reasons known only to Microsoft, that code was disabled by
default.
There is still typically some difference in speed, at least when a
compacting GC is used. Specifically, since this keeps the free memory in
the heap in one large chunk, allocation is done about like on a stack.
By contrast, with a manually managed heap that doesn't do compaction,
you end up with free blocks interspersed with blocks in use. In a really
traditional design, the allocator might round the requested size up to
the next multiple of, say, 16 bytes, and then walk through the list of
free blocks to find one large enough to satisfy the request. When it
found one, it would check the current size of the block, and if it
exceeded the requested size by at least some margin, it would split the
block in two, satisfying the request with one and placing the other back
on the free list.
More recent designs do things like rounding requests to powers of two
(starting from 16 or so), and keeping a separate free list for each
size. Once you've rounded up the size, you search in the correct list,
and either a block is there, or it isn't. If it isn't, you go to the
next larger block size, split that block in half, satisfy the request
with one half, and place the other half on the previously-empty free
list for its new size.
The former version can be quite slow, especially if there are a lot of
free blocks in the system. As you can probably guess, the latter can
improve speed quite a bit.
--
Later,
Jerry.
The universe is a figment of its own imagination.