Garbage collector question

V

Vincent Cantin

If there is no references exept a WeakReference to an object and I call
System.gc(), am I sure that my object has been garbage collected ?
 
B

Babu Kalakrishnan

Vincent said:
If there is no references exept a WeakReference to an object and I call
System.gc(), am I sure that my object has been garbage collected ?

If a get() call on the WeakReference returns null, the object has been GC'd.

BK
 
V

Vincent Cantin

If there is no references exept a WeakReference to an object and I call
If a get() call on the WeakReference returns null, the object has been
GC'd.

It doesn't answer my question.
 
G

Gordon Beaton

GC'd.

It doesn't answer my question.

There is no guarantee that gc will collect every object that is
available to be collected, so the answer to your question is no.

/gordon
 
X

xarax

Gordon Beaton said:
There is no guarantee that gc will collect every object that is
available to be collected, so the answer to your question is no.

System.gc() is not guaranteed to perform any collections.
So, the first answer is the correct response to your original
question. The only way to know that the instance was collected
is when WeakReference.get() returns null (or watch for the
WeakReference to be enqueued on the ReferenceQueue).
 
W

Will Hartung

Vincent Cantin said:
If there is no references exept a WeakReference to an object and I call
System.gc(), am I sure that my object has been garbage collected ?

I'm curious why you care? Why is it important one way or the other?

If you're using a WeakReference, you've already communicated to the system
that your interest in the object is temporal based on the whim of the JVM
and the GC, so why are you interested in the specific behavior? How does
this affect your applicaton?

Regards,

Will Hartung
([email protected])
 
V

Vincent Cantin

If there is no references exept a WeakReference to an object and I call
I'm curious why you care? Why is it important one way or the other?

If you're using a WeakReference, you've already communicated to the system
that your interest in the object is temporal based on the whim of the JVM
and the GC, so why are you interested in the specific behavior? How does
this affect your applicaton?

Regards,

Will Hartung
([email protected])

because I need to know in some points of given class of programs that an
object is reachable or not.
 
A

Ann

Vincent Cantin said:
because I need to know in some points of given class of programs that an
object is reachable or not.
What happens if you compare against null?
 
C

Chris Smith

Vincent said:
because I need to know in some points of given class of programs that an
object is reachable or not.

If you want to know whether the WeakReference is still valid, just use
get() and compare the result with null. You are not guaranteed that a
call to System.gc will clear the WeakReference, so you need to check
yourself if you wish to find out.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
V

Vincent Cantin

because I need to know in some points of given class of programs that an
If you want to know whether the WeakReference is still valid, just use
get() and compare the result with null. You are not guaranteed that a
call to System.gc will clear the WeakReference, so you need to check
yourself if you wish to find out.

I think nobody understood the problem : What I want to know is wether or not
my object is reachable.

To check the value returned by of the WeakReference.get() will not tell me
what I want to know, since if the answer is not null it doesn't give me the
information about the reachability of the object.

I don't a way to know when a weak reference reflect the reachability of the
object ...
understood ?
 
V

Vincent Cantin

because I need to know in some points of given class of programs that an
If you want to know whether the WeakReference is still valid, just use
get() and compare the result with null. You are not guaranteed that a
call to System.gc will clear the WeakReference, so you need to check
yourself if you wish to find out.

In fact yes, I will have to find myself .. i.e. I will have to define a
language interpreted by my java program and handle myself the references.
 
C

Chris Uppal

Vincent said:
I think nobody understood the problem : What I want to know is wether or
not my object is reachable.

There is no way that you can do that.

Not without utilising special knowledge about the behaviour of your program
that the Java runtime probably doesn't have. (E.g. consider what happens if
reachablility is affected by different threads.)

What Java /does/ give you is the ability to find out when the Java runtime has
/in fact/ found out that an object is no longer reachable by strong references.
There are various related mechanisms for doing that, aimed at different kinds
of application.

Since there are few reasons ever to want to know /immediately/ whether an
object is reachable (I'm not saying there are none, but I can't think of any
off-hand) I feel that it is likely that whatever you are trying to do would be
better achieved in some other way.

So what /are/ you trying to do ?

-- chris
 
B

Babu Kalakrishnan

Vincent said:
I think nobody understood the problem : What I want to know is wether or not
my object is reachable.

To check the value returned by of the WeakReference.get() will not tell me
what I want to know, since if the answer is not null it doesn't give me the
information about the reachability of the object.
The only sureshot way I can see is to ue a heap profiler.

http://java.sun.com/j2se/1.4.2/docs/guide/jvmpi/jvmpi.html

BK
 
A

Adam

because I need to know in some points of given class of programs
I think nobody understood the problem : What I want to know is
wether or not
my object is reachable.

To check the value returned by of the WeakReference.get() will not
tell me
what I want to know, since if the answer is not null it doesn't give
me the
information about the reachability of the object.

Object yourObject = weakReference.get();
if(yourObject != null) System.out.println("Yes, your object is
reachable at this very moment,
and at least as long as 'yourObject' variable refers it ");
I don't a way to know when a weak reference reflect the reachability
of the
object ...
understood ?

Nope.
 
T

Thomas Richter

Hi,
because I need to know in some points of given class of programs that an
object is reachable or not.

Then, of course, the next question should be:

Why do you want to know at some some in your program that a given class
is reachable?

Frankly, I believe there's a conceptional error in a program requiring
this kind of information to work properly. But without knowing more, it's
hard to judge.

So long,
Thomas
 
C

Chris Smith

Vincent said:
I think nobody understood the problem : What I want to know is wether or not
my object is reachable.

There is no way to find this out. It's simply not feasible. To
suddenly determine whether an object is reachable on demand would
require insane amounts of work. It could take several seconds for some
applications. The only reason most garbage collection algorithms are at
all workable is that they defer this work.

A WeakReference will be cleared as soon as the VM realizes that the
object isn't reachable through other means. Therefore, you *can* find
out whether the VM has determined that the object is unreachable or not
-- and WeakReference is one way to do it. However, it's always possible
that the VM has not yet realized that the object isn't reachable. There
is no way in Java to force the VM to do a full garbage collection and
check that, although System.gc suggests that it try.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
X

xarax

Vincent Cantin said:
In fact yes, I will have to find myself .. i.e. I will have to define a
language interpreted by my java program and handle myself the references.

When the GC places the WeakReference onto the ReferenceQueue,
then the object is unreachable, but you have to call weakReference.clear()
when you pull the WeakReference off of the ReferenceQueue. Use
a separate thread to wait on the ReferenceQueue. Note that using
get() after the WeakReference has been put onto the ReferenceQueue
will make the object strongly reachable again, so use some other
means to identify indirectly the object when you see its WeakReference
wrapper on the ReferenceQueue.
 
C

Chris Smith

xarax said:
Note that using
get() after the WeakReference has been put onto the ReferenceQueue
will make the object strongly reachable again

That's not my interpretation of the spec, which says:

Suppose that the garbage collector determines at a certain point in
time that an object is weakly reachable. At that time it will
atomically clear all weak references to that object and all weak
references to any other weakly-reachable objects from which that
object is reachable through a chain of strong and soft references.
At the same time it will declare all of the formerly weakly-
reachable objects to be finalizable. At the same time or at some
later time it will enqueue those newly-cleared weak references that
are registered with reference queues.

In other words, any WeakReference that's added to a reference queue has
already been cleared, and a call to get() will return null.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
X

xarax

Chris Smith said:
That's not my interpretation of the spec, which says:

Suppose that the garbage collector determines at a certain point in
time that an object is weakly reachable. At that time it will
atomically clear all weak references to that object and all weak
references to any other weakly-reachable objects from which that
object is reachable through a chain of strong and soft references.
At the same time it will declare all of the formerly weakly-
reachable objects to be finalizable. At the same time or at some
later time it will enqueue those newly-cleared weak references that
are registered with reference queues.

In other words, any WeakReference that's added to a reference queue has
already been cleared, and a call to get() will return null.

Oops, you're right. I had some confusion about
the special behavior of PhantomReferences.
 
V

Vincent Cantin

Object yourObject = weakReference.get();
if(yourObject != null) System.out.println("Yes, your object is
reachable at this very moment,
and at least as long as 'yourObject' variable refers it ");

No .. this means weakly reachable, and *perhaps* no longer simply reachable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top