wood burning stoves 2.0*
The moose likes Java in General and the fly likes Memory accocation in case of arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Memory accocation in case of arrays" Watch "Memory accocation in case of arrays" New topic
Author

Memory accocation in case of arrays

Vrushali Gore
Ranch Hand

Joined: Oct 17, 2005
Posts: 40
In case of array of objects where exactly memory is allocated.
What we say is array reference will be created onto stack, that's fine.
This array reference in turn points to object refernces. In which
area of memory they are allocated?(Either heap or stack)
karthikeyan Chockalingam
Ranch Hand

Joined: Sep 06, 2003
Posts: 259
I understand that always objects will be allocated memory in heap. Please correct me if i am wrong.


http://www.skillassert.com


Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
Arrays are Objects so they live in Java Heap.

  • instance member variables (fields) are on heap (both primmitive and reference)
  • local variables are on stack (both primmitive and reference) - they are all part of some object
  • class (ie. static) fields are on heap (permanent generation)

  • Ernest Friedman-Hill
    author and iconoclast
    Marshal

    Joined: Jul 08, 2003
    Posts: 24183
        
      34

    Moving to Java in General (Intermediate).


    [Jess in Action][AskingGoodQuestions]
    Vrushali Gore
    Ranch Hand

    Joined: Oct 17, 2005
    Posts: 40
    Thanks for your answer to my question. But I wanted it more clearly. I would be really very thankful if u plz help me out. When we create an array ref. it is allocated on stack because it's a ref. That ref. in turn points to object referenes(If it is array of objects). Here exactly I want to know where actually this array of refs would be created.
    Then in turn these refs would refer to the objects which would be created on heap.
    Rajashekar Subramany
    Greenhorn

    Joined: Nov 16, 2005
    Posts: 17

    Moose [] mooseArray = new Moose[5];

    You are right with Array reference living in the stack (e.g. mooseArray). Now if that Array is an array of objects, i.e. a number of reference types in the array object, they would all belong in the heap.

    Just as how instance variables reside in the heap with an object, despite it being a primitive type or reference type, the array elements also reside in the heap regardless of it being a primitive or reference type.

    mooseArray[0]
    mooseArray[1]
    mooseArray[2] >> All in the heap
    mooseArray[3]
    mooseArray[4]



    This is what i think. Do correct me if am wrong.


    S. Rajashekar
    Ernest Friedman-Hill
    author and iconoclast
    Marshal

    Joined: Jul 08, 2003
    Posts: 24183
        
      34

    Originally posted by Rajashekar Subramany:
    This is what i think. Do correct me if am wrong.


    You're correct in your thinking, although your example has problems.

    I think what confuses people is the oft-repeated, but frankly wrong, statement that "references are on the stack, and objects are on the heap." What goes on the stack are local variables. Any variable that you declare inside a method is on the stack. This is true for all kinds of variables, primitive and refererence. It has nothing to do with the type of the variable -- only where it's defined.

    Instance variables -- non-static variables declared outside of any method -- are part of an object. They are the object, in fact. When we say that "an object is allocated on the heap", what we're saying is that memory to hold the instance variables of that object is allocated in the heap area. Again, the data type of the variables is irrelevant: primitive members and reference members alike are stored in the object's same little block of memory on the heap.

    Anything created by "new" is an object; this include arrays. Arrays are instances of special classes that extend java.lang.Object, just like any other class. These objects, just like all other objects, are allocated on the heap, and that allocated memory contains all their data. The array elements are like the "member variables" of the array, and as such, they are part of the object living on the heap. I should say here, to be precise, that nowhere in the JLS or VM spec does it say anything about how objects are laid out in heap memory. You have to realize that much of what we're saying here is conceptual rather than a description of some physical reality in the computer.

    Anyway, based on the "mooseArray" example, we can't tell where mooseArray is, because if we only see that one line of code, we don't know if it's in the body of a method or at class level, and it's really this kind of one-line example that gives rise to the confusion we're trying to remedy.

    The short answer: arrays, including the element data they contain, are objects allocated on the Java heap.
    [ January 21, 2006: Message edited by: Ernest Friedman-Hill ]
    Rajashekar Subramany
    Greenhorn

    Joined: Nov 16, 2005
    Posts: 17

    Wow! nice explanation. Thanks!
    Vrushali Gore
    Ranch Hand

    Joined: Oct 17, 2005
    Posts: 40
    Thanks for your very nice explaination in the concept. Even the concept cleared how exactly memory allocation goes in.

    Once again thanks a billion
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Memory accocation in case of arrays
     
    Similar Threads
    Primitive variable
    How Memory is Allocated in Java when Instantiating an Object
    Array doubt
    can explain about heap and pool variables?
    array-constructor