• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reference Objects from java.lang.ref

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/
reply
    Bookmark Topic Watch Topic
  • New Topic