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?
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.
No. HashSet is a wrapper for HashMap so it consumes the same amount of memory.
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.