Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle SQL syntax check  without executing 6 104
use lov values 2 71
Eclipse for Java EE development 2 43
Java Eclipse Loop 3 29
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…

763 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