Solved

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

Posted on 2003-10-30
5
273 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 99
numbers ascending pyramid 101 195
print map entry 34 69
javap not working 8 46
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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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.

809 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