3

Thinking about this question, I don't think it would be bad since object references only take up 4 bytes of memory (in a 32-bit JVM), but intuitively I feel like I'm doing something wrong when I have many (+100) references to the same object. This usually happens when I create a certain class +100 times and each need to hold the reference.

I know I can re-design my code in most cases to avoid this, but sometimes its much easier to just keep the reference within each object.

Anyway, is it bad to have many references to the same object?

srobinson
  • 346
  • 2
  • 4
  • 18
  • 3
    No, there's nothing wrong with that. – Louis Wasserman Jul 25 '14 at 18:28
  • 1
    Have you objectively found out any problems related to it? – Jeroen Vannevel Jul 25 '14 at 18:28
  • Why references always take 4 bytes of memory,is it really fixed??? – Am_I_Helpful Jul 25 '14 at 18:30
  • @Scott-What do you mean by `I create a certain class +100 times`? In a same project??? – Am_I_Helpful Jul 25 '14 at 18:31
  • @shekharsuman according to [this](http://stackoverflow.com/questions/3733215/memory-requirements-of-an-object-reference-on-a-64-bit-jvm) it is 4 bytes in 32-bit JVM and 8 bytes in 64-bit. I should have said "instantiate certain classes" instead of "create" – srobinson Jul 25 '14 at 18:32
  • @shedker - I think he means instantiate the same class several hundred times? – miss.serena Jul 25 '14 at 18:32
  • @Jeroen No I have not, but I feel like there could be some unintended consequences that I'm not seeing, which is why I'm asking – srobinson Jul 25 '14 at 18:34
  • 3
    No, it's not bad to have multiple references to the same object if there's no better way to structure your application. But make sure to check whether there's a better way to structure things to avoid doing this needlessly. The amount of memory might be small, but it could be a sign that your structure is not as elegant as it could be. – Bobulous Jul 25 '14 at 18:34
  • @Arkanon-You would better frame an answer so that I can upvote and others can get an insight! – Am_I_Helpful Jul 25 '14 at 18:36
  • @shekharsuman - The size of a reference is not specified. It's merely sufficiently large to enumerate all possible objects in the object heap. – Hot Licks Jul 25 '14 at 18:49
  • Say,an Object is of size 10 bytes,in that Class will the instance of 4 bytes pointing to that Object be sufficient,SORRY to disturb again as I am not learning this @HotLicks! – Am_I_Helpful Jul 25 '14 at 18:59
  • In most realistic scenarios an object will be a minimum of 16-32 bytes in size. But there's no theoretical connection between object size and reference size, other than the object must be large enough to physically contain all the references and other values it holds. – Hot Licks Jul 25 '14 at 19:04
  • And, for extra credit, figure out how several JVMs manage to address a 32G heap with 32-bit references. – Hot Licks Jul 25 '14 at 19:06
  • Probably this is more of a Programmers than StackOverflow question since it's a little broad and open-ended for SO – Sled Jul 25 '14 at 20:40

2 Answers2

1

Having many references to the same object is unavoidable, but has no dissadvantage IMHO.

Every Class has a reference to it from every instance of that class. Each class loader has a reference from every class. The empty String is often the most referenced object with tens of thousands of references being common.

I know I can re-design my code in most cases to avoid this, but sometimes its much easier to just keep the reference within each object.

I suggest you do what you believe is simplest and clearest and this will be easiest to maintain.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Yeah, usually simpler is better. – Hot Licks Jul 25 '14 at 18:50
  • 2
    @HotLicks Sometimes there is a very good reason for something more complicated, but without more information, the aim should be to as simple as you can make it and come back later and make it simpler again. – Peter Lawrey Jul 25 '14 at 18:54
1

Thinking about this question, I don't think it would be bad since object references only take up 4 bytes of memory (in a 32-bit JVM), but intuitively I feel like I'm doing something wrong when I have many (+100) references to the same object.

From a performance/resource utilization standpoint, references are waaaaaaaaaaaaaaaay more efficient than creating and destroying objects. Lots of ittybitty objects can fragment the heap and tax the memory manager/garbage collector. This is why it's usually worth the effort to make immutable objects singletons. Construction of even small objects in Java is more expensive than using references.

Most programs won't notice any significant difference, but some will.

This usually happens when I create a certain class +100 times and each need to hold the reference.

If every instance of a class references that object, use a static rather than instance variable to store the reference. You can use a static initializer to allocate it, or create a factory method to instantiate objects of the class and have the factory method allocate the referenced object the first time it is invoked.