my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Reference Objects from java.lang.ref Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reference Objects from java.lang.ref" Watch "Reference Objects from java.lang.ref" New topic
Author

Reference Objects from java.lang.ref

Arthur Blair
Ranch Hand

Joined: Sep 20, 2005
Posts: 71
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

Joined: Aug 16, 2005
Posts: 14278
    
  21

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).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Arthur Blair
Ranch Hand

Joined: Sep 20, 2005
Posts: 71
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
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Sep 20, 2005
Posts: 71
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/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reference Objects from java.lang.ref