• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reference Objects from java.lang.ref

 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still working my way through Bruce Eckels Thinking in Java and have come up with a query about reference objects.

Bruce says: You use reference objects when you want to continue to hold onto a reference to that object, but you also want to allow the garbage collector to release that object. Thus, you have a way to go on using that object, but if memory exhaustion is imminent, you allow that object to be released.

I thought that once the garbage collector releases an object it is gone for good. If the garbage collector releases the object, then the memory that holds the object gets cleared so it can be used again. So how can you keep a reference to an object and get the garbage collector to release it at the same time?

If the garbage collector releases the memory, what is left for the Reference point to?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15482
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought that once the garbage collector releases an object it is gone for good.
That is still true.

So how can you keep a reference to an object and get the garbage collector to release it at the same time?
That's not what happens. The garbage collector does not clean up objects that are not used anymore immediately. It runs every now and then, and even when it runs it might not necessarily clean up everything that's waiting to be cleaned up.

When you use a reference object, you tell the garbage collector that it may cleanup the object, but as long as it hasn't done this, the object is still accessible. When the garbage collector really does cleanup the object, you can't reference it anymore, and the reference object will get invalidated (how this works exactly I don't know, because I never used reference objects myself).
 
Henry Wong
author
Marshal
Pie
Posts: 21496
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no black magic here. As long as you have a hard reference to an object, the GC won't touch it. To effectively use a reference object, you must also get rid of your hard references to that object.

Then when you need it again, get() the original object from the reference object, and ... you'll either get a hard reference to use again, or you get null returned.

Henry
 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I appreciate the replies guys. I understand what you are saying.

When the garbage collector runs, if an object has a normal reference then it won't be collected. While if it has a java.lang.ref.Reference then there is a chance that the garbage collector will free up its memory.

But if you say: I don't need this object now, so I'll remove the normal reference and set a java.lang.ref.Reference; you will never know whether or not the java.lang.ref.Reference actually points to a valid object.

I don't understand why you would want a reference to an object to be weak. What is the point of having a reference to an object that you might lose, but you don't know when you will lose it?

Could you give me an example of when would actually need to use a java.lang.ref.Reference?
 
Henry Wong
author
Marshal
Pie
Posts: 21496
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you give me an example of when would actually need to use a java.lang.ref.Reference?


Classic example is that of a cache. It contains objects that you may need again. But can be retrieved from a *slow* database if necessary. You would like this cache to be as big as possible since the database is slow -- but you don't want to run out of memory doing it.

Henry
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arthur Blair:
Ok, I appreciate the replies guys. I understand what you are saying.

When the garbage collector runs, if an object has a normal reference then it won't be collected. While if it has a java.lang.ref.Reference then there is a chance that the garbage collector will free up its memory.

But if you say: I don't need this object now, so I'll remove the normal reference and set a java.lang.ref.Reference; you will never know whether or not the java.lang.ref.Reference actually points to a valid object.

I don't understand why you would want a reference to an object to be weak. What is the point of having a reference to an object that you might lose, but you don't know when you will lose it?

Could you give me an example of when would actually need to use a java.lang.ref.Reference?



What is this java.lang.ref.Reference???
How can I make this reference to point out my object???
What is the use of it???

If I don't need object then I should release it and if I need then I shouldn't. Why should I will be in uncertainity???

I hope I am making valid point here.

Thanks.


 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again Henry, I think it has clicked now with the cache example.

Rathi, read the References section in chapter 11 of Bruce Eckels Thinking in Java. The code exercises there, coupled with this webpage should make things clear.

Its downloadable: http://www.mindview.net/Books/TIJ/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic