The Artima Developer Community
Sponsored Link

Java Answers Forum
Java Object References

6 replies on 1 page. Most recent reply: Mar 24, 2002 12:37 PM by Mohit Gupta

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Mohit Gupta

Posts: 15
Nickname: mohit
Registered: Mar, 2002

Java Object References Posted: Mar 20, 2002 2:22 PM
Reply to this message Reply
Advertisement
is there any way using which we can know about any object that whether it has any active reference at present or not, means qualify for gc or not. and if so then can we get the information of its referrent?


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Java Object References Posted: Mar 20, 2002 2:30 PM
Reply to this message Reply
if your object is null, then the garbage collector may garbage collect it.
for Object o for example you can test if it is null
if (o == null) ...

Its a good habit of setting objects to null after you know you are done with them so the garbage collector will hopefully collect them. It is my understanding that the actual garbage collection may or may not actually happen.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Java Object References Posted: Mar 20, 2002 10:27 PM
Reply to this message Reply
Well, really an object is never null. Although null may be an object (especially in Ruby). ;-) A reference to an object may be set to null, but that doesn't tell you anything about whether the object has other references.

It is kind of a conundrum; if you wanted to ask "are there any references to this object?" then how would you specify which object without a reference to it? I suppose, you could ask "how many references are there to this object?" and if the answer is one, then when you set your reference to null, the object no longer has references (so it will probably have a hard time getting a job). This assumes the method that answered the question was truthful and didn't sneakily add a reference!

Anyhow, this is probably JVM internals stuff, so we'll have to wait for Bill to weigh in on this...

Now, I would like to now congratulate myself on completing this disquisition without resorting to the questionable practice of using "reference" as verb. Thank you, thank you very much.

Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: Java Object References Posted: Mar 21, 2002 11:56 AM
Reply to this message Reply
> is there any way using which we can know about any
> object that whether it has any active reference at
> present or not, means qualify for gc or not. and if
> so then can we get the information of its referrent?

If an object has no active reference, then you can't have a reference to it either, so the short answer is no. If you have a reference to it, then it is by definition not available for garbage collection. In other words, if you know enough about an object to ask the question is it available for GC, you probably have a reference to it so it isn't available for GC.

Maybe what you really are after is phantom references. Since 1.2, the JVM does support soft, weak, and phantom references. I'm not sure what you are trying to accomplish, but phantom references do allow you to be notified after object becomes available for garbage collection and before it is collected. More info about soft, weak, and phantom references is available here:

http://www.artima.com/insidejvm/ed2/ch09GarbageCollection14.html

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Java Object References Posted: Mar 21, 2002 1:16 PM
Reply to this message Reply
Bill, I think you can get the reference of an object back even if there is no reference to it. I know this sentence sounds weird, but you can do that as follows. When you set all object references to an object to null or all object references to an object go out of scope then that object becomes a candidate for garbage collection. What I am saying is when an object becomes a candidate for garbage collection then also you get one chance to get it back and then you can work with it again. Here is how it works. Before garbage collecting the object, JVM will first run finalizers for that object. Override the finalize method for that class of that object and then pass its reference to a method in other object which will store that reference. This way when finalize() method for that orphaned object is called it gets a reference to point to it. JVM won't just garbare collect this object because finalize() method has already been called on this object. Before garbase collecting JVM again checks if there is any reference that exists to that object then garbage collection is postponed at that time.
I agree that currently there is no API in Java which tell you how many referenes are there for an object.
Thanks
Kishori
 
class GetObjectBack {	
	static T1 t ;
	public static void main ( String[] args ) throws Exception {
		int count = 0 ;
		while ( true ) {
			// Create orphan T1 objects
			new T1() ;
			
		}	
	}
	
	public static void setItBack ( T1 t1 ) {
		//  Here you can store the reference back 
		t = t1 ;
		t.lastMessage() ;
		System.out.println ( "Got back :" + t.name ) ;
		System.exit(0) ;		
	}
}
 
class  T1 {
	public int[] temp = new int[200] ;
	public String name = "" ;
	
	{
		name  = "N-" + System.currentTimeMillis ( ) ;
	}
	
	public void finalize ( ) {
		System.out.println ( "Finalize is being triggered for:" + name ) ;
		GetObjectBack.setItBack ( this ) ;	
	}
	public void lastMessage() {
		System.out.println ( " I have been finalized already.... Name:" + name ) ;
	}
}
 

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Java Object References Posted: Mar 21, 2002 1:31 PM
Reply to this message Reply
Bill's article, which he referred to above (see the URL in his post), discusses this point; a referece is called "resurrectable" before its finalizer has run.

The article also goes into more detail about SoftReference, WeakReference, and PhantomReference objects, which allow you to have fancier management of references.

Mohit Gupta

Posts: 15
Nickname: mohit
Registered: Mar, 2002

Re: Java Object References Posted: Mar 24, 2002 12:37 PM
Reply to this message Reply
Thanks for ur reply, actually this the phantom reference which i need, but as i am dealing first time with references so not know about phantom reference,
and also this is the same reason that my question was not clear. now studying about it. besides it, is there any tutorial onine which help me in managing memory more efficiently or helps me in performance optimization ??

Flat View: This topic has 6 replies on 1 page
Topic: is this AND valid? Previous Topic   Next Topic Topic: Multi-Threading and counters question

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use