Solved

Does Java have anything akin to a smart pointer for referenced objects?

Posted on 2003-10-30
5
272 Views
Last Modified: 2010-05-18
I am using hashtables to hold shared values (resources).  When the values stored in the hashtable are no longer referenced externally, I wish to have the value reference in the hashtable removed so the value object can be garbage collected.  Thus I have two related questions:
1) How in Java can one tell how many references there are to a given object?
2) Is there some Java equivalent to a C++ smart pointer class but for references?
0
Comment
Question by:Taurus
  • 3
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9653719
1. The JVM knows what references are being kept
2. Not necessary since objects no longer referenced are garbage collected


0
 

Author Comment

by:Taurus
ID: 9653856
The hashtable reference to a given value object will not be released.   I know there is a so called weakhashtable but I'm looking for something akin to a smart pointer class.  I need to know when these values are no longer being referenced from "outside" the hashtable and my POS.
0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9654256
Java objects pretty much behave like smart pointers themselves.  There are many types of smart pointers, and they all do different things, but there are two main functionalities they are used for.

1.) Reference counting.  This is unnecessary for the java programmer, because the JVM keeps track of reference counts, and deletes the object automatically when it reaches 0.

2.) Auto deleting objects referenced by the smart pointer when the pointer is deleted.  Java automatically does this as well.  Let's say you have a java object that references another java object.  When the first object loses all it's references, it will be deleted by GC.  The second object will automatically get deleted as well assuming it is not being referenced by another object somewhere else.

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 100 total points
ID: 9654376
>>
I need to know when these values are no longer being referenced from "outside" the hashtable and my POS.
>>

Not sure what you mean by 'POS' but you might be able to do this using reference objects and a reference queue. Instead of your object being the value in the hash table it would be the referent of a reference object. The reference object would be created in conjunction with a reference queue and then added as the key's value in the hash table. When the value gets garbage collected it will be added to the reference queue. This can then be polled. Null objects can then have their keys and values (the reference objects) removed. Hey presto, no lingering objects!

Here's an article on something similar:

http://builder.com.com/5100-6386-1049546.html

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9674258
:-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now