Hi,
I was previous under the impression that allocating a single large block of
memory in Java
is faster (possibly much faster) than allocating lots of the smaller blocks
whose total storage space
equals that of the large block.
so 1 MB in a single allocation is faster than 10000 allocations of 100 byte
blocks.
Also the garbage collection performance goes way up as well?
Could you please comment on whether this is right, or in th ballpark?
I was reading some one's paper saying that object allocation time is linear
with the the size of the allocation,
which counters my intuition.
Thank,s
Jimmy
Roedy Green - 31 Jul 2003 22:53 GMT
>I was previous under the impression that allocating a single large block of
>memory in Java
[quoted text clipped - 3 lines]
>so 1 MB in a single allocation is faster than 10000 allocations of 100 byte
>blocks.
Allocating a large block can be problematic. There may be no hole big
enough to fit. You have to do a garbage collection or compaction to
make room.
Obviously there is more overhead allocating 10 small objects than one
big one given that room can be found without gc.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Mark Bottomley - 31 Jul 2003 23:12 GMT
Jimmy:
Multiple allocations will be slower as you go through the allocation
code that many more times, but the ratio may not be what you expect. There
is a linear component to allocations within Java in that it requires that
all uninitialized fields of an object be set to zero for primitives and Null
for references. This initialization means that a large allocation's timing
may be dominated by the clearing of memory. The performance ratio will be
set by the VM's implementation of memory allocation. If the VM sub-allocates
out of a large memory chunk, the timing may be faster, but if it allocates
from the OS's allocator, then there will be additional overhead from
acquiring a global lock for memory access.
Garbage collection is another area where the type of collector plays a
big role. All collectors will spend a lot of time either incrementally or
bulk scanning of the thread stacks and heaps to determine object liveness
(hence collectability). More objects - more time.
Java however does not let the user directly allocate memory any ways
except as an object or some variant of an array. The most common ways of
avoiding many allocations in systems that care about it are using object
pools to manage the memory yourself (assuming you know upper limits) and
implementing multidimensional arrays as larger linear arrays with
row/column/plane calculations performed by the software instead of the
internal walking of arrays of arrays of arrays of ... arrays of
primitives/objects.
The better answer to this would be for you to explain your problem and
why memory allocation is troublesome.
Mark...
> Hi,
>
[quoted text clipped - 16 lines]
> Thank,s
> Jimmy