0

I want to improve my knowledge about memory model of programming languages (particulary in Java), so I have one question.

Here is very simple code:

// Allocating memory in heap for SimpleObject's instance
// Creating reference to this object with name so1
SimpleObject so1 = new SimpleObject();


// Allocating memory in heap for array of 10 references to SimpleObject's objects
// Now I know, that array stores only references to the objects
// (Previously I thought that array stores objects)
// Then we create reference to this array with name soArray
SimpleObject[] soArray = new SimpleObject[10];

Now the question:

// What is going on here?
soArray[0] = so1;
// object so1 had been really moved to memory area of soArray?
// And so1 reference have been updated to new memory address?

// Or we just had assigned so1 object's reference to soArray[0] element?

// Or so1 object had been copied to the soArray[0]? 
// Then original so1 object has been deleted and all links to it had been updated?

If you know, how it works in other languages, such as (C, C++, C# or other), please answer, I will be glad to know it.

Everybody know, that ArrayList can be faster than LinkedList, because elements of array could be stored in CPU cache, while if we working with LinkedList, CPU has to get next object from RAM each time.

So how could it work, if at first I had created object in heap and only then I had put object in array?

UPD: Thank you guys, now I understand how array is working, but what about caching array in CPU cache in that way?

1
  • 1
    An array of objects only stores the references to the objects. It does not store the whole object in the array. Commented May 18, 2013 at 21:26

3 Answers 3

4

Arrays store references to objects, not the objects themselves. You therefore swap the reference at position 0 when assigning soArray[0]. The objects themselved can be moved within the heap, but this is usually due to GC, not assignments.

If the objects themselves were stored directly in the array, you could not have instances of subclasses with more instance fields in your array. They would not fit into the allocated space and therefore only become instances of the base class. This is what actually happens in C++ when you assign class instances stored on the stack.

Sign up to request clarification or add additional context in comments.

2 Comments

Great answer, thank you. But what about caching array in CPU cache? It could be possible only if GC will move objects one after another? If so, languages without garbage collection could not perform array caching for CPU?
@ArtemZinnatullin I don't know, to be honest. As far as I know though memory caching is page based, so objects "just" need to be close to each other in memory. This could be achieved without garbage collection too if they're placed right in the first place (guaranteed when stored in-place, see Steve's answer stackoverflow.com/a/16629274/17713). Again, I'm happy with comments and edits though, as I'm unsure about the details here.
2

In Java, arrays store references to objects. In C++ parlance they store pointers to objects.

SimpleObject[] soArray = new SimpleObject[10];   //Java
SimpleObject* cppArray[10];                       // C++ equivalent

soArray[0] = so1; puts a reference to so1 in soArray[0] in the same way that cppArray[0] = &so1 stores a pointer to so1. The original object remains unchanged, no additional memory is allocated or deallocated.

In C++ you can store the objects directly in an array.

SimpleObject soArray[10];                       // An array that stores Simple Objects in place
SimpleObject so1;                               // A new object
soArray[0] = so1;                               // This *copies* so1 into soArray[0]

Comments

-1

We assign the reference to the object pointed by so1 to the array element.

Here's an example in using Python Tutor (there's no equivalent tool for Java that I know, but the memory model is similar, except for the Class being an object, so ignore that):

Example in Python

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.