• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Memory accocation in case of arrays

 
Vrushali Gore
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that always objects will be allocated memory in heap. Please correct me if i am wrong.
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Pie
    Posts: 24212
    35
    Chrome Eclipse IDE Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Moving to Java in General (Intermediate).
     
    Vrushali Gore
    Ranch Hand
    Posts: 40
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 17
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Ernest Friedman-Hill
    author and iconoclast
    Marshal
    Pie
    Posts: 24212
    35
    Chrome Eclipse IDE Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 17
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Wow! nice explanation. Thanks!
     
    Vrushali Gore
    Ranch Hand
    Posts: 40
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for your very nice explaination in the concept. Even the concept cleared how exactly memory allocation goes in.

    Once again thanks a billion
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic