array initilization and memory usage  
Author Message
jon23d





PostPosted: 2006-6-26 13:12:00 Top

java-programmer, array initilization and memory usage 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





PostPosted: 2006-6-26 16:01:00 Top

java-programmer >> array initilization and memory usage 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.

<email***@***.com> a 閏rit dans le message de
news:email***@***.com...
> 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
>


 
ge0rge





PostPosted: 2006-6-26 17:38:00 Top

java-programmer >> array initilization and memory usage Xavier Tarrago wrote:
> <email***@***.com> a 閏rit dans le message de
> news:email***@***.com...
>
>>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
>>
> 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.
>

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.

--
It's not the inital skirt length, it's the upcreep.
 
 
Frank van Schie





PostPosted: 2006-6-27 1:50:00 Top

java-programmer >> array initilization and memory usage ge0rge wrote:
> > 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.
> >
>
> 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.

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





PostPosted: 2006-6-27 6:07:00 Top

java-programmer >> array initilization and memory usage Frank van Schie wrote:
> ge0rge wrote:
>> 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.
>
> 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





PostPosted: 2006-6-28 22:31:00 Top

java-programmer >> array initilization and memory usage ge0rge wrote:

> 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鯾lom