Solved

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

Posted on 2003-10-30
5
278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

636 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