Navigateur
Navigateur

Reputation: 1892

Does each shared reference take up another word of memory (e.g. 32 or 64 bits) in Java?

Let's say 3 objects each have a reference to an object x, do each of those 3 references take up another word of memory (e.g. 32 or 64 bits, meaning a grand total of 96 or 192 bits), even if they have all been set to the very same object?

e.g.

X x = getX();
object1.x = x;
object2.x = x;
object3.x = x;

?

Furthermore, if I have a HashMap<Character, Character>, where I always enter the exact same Character object as both key and value, does it take up double the memory of the references in a HashSet<Character> plus the amount of memory for the Characters themselves, even though the same object reference is being used for both key and value each time? I know this might be the same as the first question but I wonder if this case might behave any differently?

In each of these cases, if the root objects are static, are the references stored on the stack or the heap?

Upvotes: 1

Views: 69

Answers (6)

Lefteris Laskaridis
Lefteris Laskaridis

Reputation: 2322

Objects x, object1, object2 and object3 will be allocated on the heap once. Each of the last three objects will reference instance x via a reference. No additional memory will be required for each of those objects, besides the reference property (assuming they don't have any other property). Generally the space consumed for each object depends on its properties. Each allocated object requires in addition by the jvm a header, typically including a mark word, class metadata and if the instance is an array its length (see http://www.cs.princeton.edu/picasso/mats/HotspotOverview.pdf).

Statically defined class properties are allocated in the class definition located in the permanent generation (in hotspot) during the class initialization phase (http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-5.html#jvms-5.5).

Upvotes: 0

Raffaele
Raffaele

Reputation: 20875

Think of an object like a row in a database table: it doesn't matter if two rows have the same value for a given column: still the value has to be saved in the specific row, so yes if two users share the name John, your table grows to host two John's :)

+------------------------------+
|   ID   |        NAME         |
+--------+---------------------+
|0x76BA39|       John          |
+--------+---------------------+
|0xB4H821|       John          |
+--------+---------------------+

Upvotes: 0

barfuin
barfuin

Reputation: 17474

Yes, you have 3 references, so it's 3 times the memory for the reference.

The Character object takes the memory for the reference plus its content (say, a char). A char primitive would only consume the space for the primitive (which is less then just a reference on a 64bit system). If you are using the same object as key and value, then you look at two references and 1 object.

Upvotes: 1

NPE
NPE

Reputation: 500167

Basically, if you have multiple references, each reference occupies separate memory regardless of where it points.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533442

A reference always takes up the same amount of memory, typically 32-bit even on 64-bit JVMs. It doesn't get smaller just because it references the same object as another reference.

does it take up double the memory of the references

No. HashSet is a wrapper for HashMap so it consumes the same amount of memory.

In each of these cases, if the root objects are static, are the references stored on the stack or the heap?

static fields are always stored on the heap in HotSpot but AFAIK it not specified in the JLS so it could be different. It wouldn't make sense to place it on the stack as that is thread local.

Upvotes: 0

Louis Wasserman
Louis Wasserman

Reputation: 198014

In the code you write, no additional memory is being consumed that wasn't already being consumed. You're just changing the contents of the references object1.x, object2.x, and object3.x.

In the current JDK, a HashMap<Character, Character> will take up the same amount of memory as a HashSet<Character> of the same size. (Slightly less, actually.) This is independent of whether or not it maps characters to themselves.

Only method-local reference variables are stored on the stack; everything else is stored on the heap. In particular, a field of any object (regardless of whether it is static or not) will be stored, like the rest of the object, on the heap.

Upvotes: 0

Related Questions