Reputation: 13
Suppose I define a new class, say PhoneBook. I do the following:
Phonebook x = new Phonebook()
Phonebook x1=x
Phonebook x2=x
...
Phonebook x99 =x
then this won't consume much memory will it since all the 100 variables are pointing to the same phonebook?
Thanks
Upvotes: 1
Views: 293
Reputation: 136012
If those are local variables they do not consume any memory on the heap because they are on stack. The size of Java pointer is JVM implementation specific usually it is 32 bit.
Upvotes: 0
Reputation: 2717
Yes indeed you can also come to my blog Reference
What really consume memory is the first time you new
a phoneBook, and the rest of Reference
Phonebook
consume just about 4byte each.
Upvotes: -1
Reputation: 41188
Correct. Each reference will consume a small amount of memory (usually 4 or 8 bytes on 32 or 64 bit systems) and that's it.
Upvotes: 5