Array allocation

Discussion in 'Java' started by DennyOR, Nov 29, 2003.

  1. DennyOR

    DennyOR Guest

    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
     
    DennyOR, Nov 29, 2003
    #1
    1. Advertising

  2. DennyOR

    Jeffrey Palm Guest

    DennyOR wrote:

    > 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.

    > Denny
    >
    >




    --
    Jeffrey Palm --> http://www.ccs.neu.edu/home/jpalm
     
    Jeffrey Palm, Nov 29, 2003
    #2
    1. Advertising

  3. DennyOR

    Roedy Green Guest

    On Sat, 29 Nov 2003 14:34:13 -0800, "DennyOR" <>
    wrote or quoted :

    >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.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Nov 29, 2003
    #3
  4. DennyOR

    Roedy Green Guest

    On Sat, 29 Nov 2003 14:34:13 -0800, "DennyOR" <>
    wrote or quoted :

    >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?)


    they get allocated every time you say "new".

    See http://mindprod.com/jgloss/gotchas.html#ARRAY

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Nov 29, 2003
    #4
  5. DennyOR

    DennyOR Guest

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

    Denny
     
    DennyOR, Nov 30, 2003
    #5
  6. DennyOR

    Roedy Green Guest

    On Sun, 30 Nov 2003 13:33:45 -0800, "DennyOR" <>
    wrote or quoted :

    >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.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Nov 30, 2003
    #6
  7. DennyOR

    Chris Uppal Guest

    DennyOR wrote:

    > 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
     
    Chris Uppal, Nov 30, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rave

    dynamic array allocation

    Rave, Sep 25, 2003, in forum: C++
    Replies:
    5
    Views:
    628
    Kevin Goodsell
    Sep 25, 2003
  2. lestat
    Replies:
    2
    Views:
    701
    lestat
    Oct 11, 2003
  3. Ken
    Replies:
    24
    Views:
    3,874
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    991
    chris
    Oct 28, 2005
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,191
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page