Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Virtual Machine / July 2003

Tip: Looking for answers? Try searching our database.

object allocation performance question

Thread view: 
Jimmy Zhang - 31 Jul 2003 21:20 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.