Solved

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

Posted on 2003-10-30
5
270 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>
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
Comment Utility
:-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoreUp challenge 14 47
network + 7 73
array220 challenge 8 44
eclipse formatting 6 38
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

772 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