Taurus
asked on
Does Java have anything akin to a smart pointer for referenced objects?
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?
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?
ASKER
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.
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:-)
2. Not necessary since objects no longer referenced are garbage collected