Array allocation

D

DennyOR

Do arrays declared and allocated within a method get allocated on the stack?
If not, will a new array be allocated every time the method is called? (Is
there a good source for information on how the compiler allocates arrays?)

Denny
 
J

Jeffrey Palm

DennyOR said:
Do arrays declared and allocated within a method get allocated on the stack?

All arrays are allocated on the heap -- the local variable on the stack
holds a reference to this value.
If not, will a new array be allocated every time the method is called? (Is
there a good source for information on how the compiler allocates arrays?)

It could, though a JIT could optimize this away probably.
 
R

Roedy Green

Do arrays declared and allocated within a method get allocated on the stack?

no. They are always allocated on the heap. Garbage collection
eventually gets rid of them. However, smart compilers like Jet can
sometimes notice usage patters and allocate them on the stack. This
is invisible to you as a programmer.
 
D

DennyOR

I guess I better not have a method that gets called hundreds of times a
second allocating a small array for local use.

Denny
 
R

Roedy Green

I guess I better not have a method that gets called hundreds of times a
second allocating a small array for local use.

In AWT you often see the caller providing an array to the callee to
return the result in. This avoids allocating fresh ones needlessly.
 
C

Chris Uppal

DennyOR said:
I guess I better not have a method that gets called hundreds of times a
second allocating a small array for local use.

That *might* cause a problem, especially if you are running on a maxed-out
server, or a pda, but for many purposes a few hundred allocations / sec is not
going to be a problem.

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top