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 / First Aid / June 2006

Tip: Looking for answers? Try searching our database.

array initilization and memory usage

Thread view: 
jon23d@gmail.com - 26 Jun 2006 06:12 GMT
I'm a little new here, so please bear with me if this sounds like a
goofy
question!...

let's say that I have an class with 15 or so members, each taking up a
fair
amount of space - we'll call this class bars

If I create an array of 400 empty bars objects, but only instantiate
the
first five or so then am I still setting aside the memory for the other
395?

I hope that made sense!

Thanks
Xavier Tarrago - 26 Jun 2006 09:01 GMT
I don't think so. new Bar[500] will alocate an array of references. Each
bars[i] = new Bar() will allocate a Bar so you'll end with an array of 400
references and 5 Bar allocated.

> I'm a little new here, so please bear with me if this sounds like a
> goofy
[quoted text clipped - 12 lines]
>
> Thanks
ge0rge - 26 Jun 2006 10:38 GMT
>>I'm a little new here, so please bear with me if this sounds like a
>>goofy
[quoted text clipped - 16 lines]
> bars[i] = new Bar() will allocate a Bar so you'll end with an array of 400
> references and 5 Bar allocated.

Incorrect.
Bar bar[500];   allocates 500 references on the stack  - no big space
requirement  ... but each new Bar() will allocate a Bar object on the heap and
the space requirement for a Bar instance.

Signature

It's not the inital skirt length, it's the upcreep.

Frank van Schie - 26 Jun 2006 18:50 GMT
>  > I don't think so. new Bar[500] will alocate an array of references. Each
>  > bars[i] = new Bar() will allocate a Bar so you'll end with an array
[quoted text clipped - 6 lines]
> requirement  ... but each new Bar() will allocate a Bar object on the
> heap and the space requirement for a Bar instance.

That's... what he said, barring syntax errors. Except he doesn't know
the difference between 400 and 500.
Signature

Frank

Mark Space - 26 Jun 2006 23:07 GMT
>> Incorrect.
>> Bar bar[500];   allocates 500 references on the stack  - no big space
[quoted text clipped - 3 lines]
> That's... what he said, barring syntax errors. Except he doesn't know
> the difference between 400 and 500.

Probably a typo.  The 4 and the 5 key are right next to each other.

Don't forget that a reference is NOT free.  I think we already figured
out here on this list that most JVMs implement a reference as 8 bytes.
So Bar bar[500]; with no objects at all is still 4008 bytes for most JVMs.

My suggestion: use a collection.
dsjoblom@abo.fi - 28 Jun 2006 15:31 GMT
> Incorrect.
> Bar bar[500];   allocates 500 references on the stack  - no big space
> requirement  ... but each new Bar() will allocate a Bar object on the heap and
> the space requirement for a Bar instance.

Not correct about the stack allocation part. There is no way of knowing
where the Bar array (the piece of memory having space for 500
references) is allocated, unless you know the internal operations of
your JVM, but that is not portable information. Most *likely* it is
allocated on the heap as any other object. Typically, only local
primitives and references are allocated on the stack.

Regards,
Daniel Sjöblom


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.