Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are arrays of Java objects tenured?

We're trying to tweak some Oracle JVM garbage collection options and one developer tried to use -XX:PretenureSizeThreshold to make sure a large array of objects was put in Tenured right away. I'm pretty sure the assumption was that the array size equals or exceeds the total size of all the objects in it.

But in Java, aren't arrays of objects just arrays of references? I.e. each object in the array, as well as the array object itself, is separate in memory and treated as separate by the garbage collector? I think the array object can still get fairly large if there are millions of entries, but it shouldn't be anywhere near the total size of the objects it "contains" if each object is much bigger than a reference.

I think there's confusion because AFAIK, in C:

  1. It's possible to have an array of structs that really does store the structs.
  2. It's also possible to have an array of pointers to structs.

I'm pretty sure Java always uses 1. for arrays of primitive types and always uses 2. for arrays of objects, while C can use either for any type...?

What if I use an ArrayList with frequent append()s (as we are in the case at hand)? Is only the array copied, and not the objects in the array? Also, when the array is copied, even if the old array was in Tenured the new one starts in Eden, right?

like image 608
Vanessa Phipps Avatar asked Oct 16 '13 18:10

Vanessa Phipps


People also ask

How are Java arrays stored?

Memory is allocated in Heap are for the Array in Java. In Java reference types are stored in the Heap area. As arrays are also reference types, (they can be created using the “new” keyword) they are also stored in the Heap area.

Is array in Java continuous?

Arrays of the JLS (and any other chapters of JLS and JVMS related to arrays) we couldn't find any mention of memory layout constraints imposed to arrays. Thus it definitely means that array might be not continuous.

How do you pass an array of objects in Java?

To pass an array as an argument to a method, you just have to pass the name of the array without square brackets. The method prototype should match to accept the argument of the array type. Given below is the method prototype: void method_name (int [] array);

What is array of objects in Java?

We know that an array is a collection of the same data type that dynamically creates objects and can have elements of primitive types. Java allows us to store objects in an array. In Java, the class is also a user-defined data type. An array that conations class type elements are known as an array of objects.


1 Answers

But in Java, aren't arrays of objects just arrays of references?

Just references. All objects are allocated on the heap, never in arrays or on the stack (at least officially, the optimizer may use stack allocation if possible, but this is transparent).

it shouldn't be anywhere near the total size of the objects it "contains" if each object is much bigger than a reference.

Yes, in Java whenever you say "assign/store an object", you mean the reference (pointer in C terminology).

What if I use an ArrayList with frequent append()s (as we are in the case at hand)? Is only the array copied, and not the objects in the array?

The array gets only copied when resizing is needed, i.e., very rarely and the amortized cost is proportional to the number of inserts. The referenced objects gets never copied.

Also, when the array is copied, even if the old array was in Tenured the new one starts in Eden, right?

Yes!

like image 122
maaartinus Avatar answered Oct 02 '22 14:10

maaartinus