Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Garbage collector question

Reply
Thread Tools

Garbage collector question

 
 
Vincent Cantin
Guest
Posts: n/a
 
      09-24-2004
> > 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.


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.


 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      09-24-2004
Vincent Cantin wrote:

> 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


 
Reply With Quote
 
 
 
 
Babu Kalakrishnan
Guest
Posts: n/a
 
      09-24-2004
Vincent Cantin wrote:
>>>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.

>
>
> 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/...mpi/jvmpi.html

BK
 
Reply With Quote
 
Adam
Guest
Posts: n/a
 
      09-24-2004
>> > 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.

>
> 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.


 
Reply With Quote
 
Thomas Richter
Guest
Posts: n/a
 
      09-24-2004
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


 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      09-24-2004
Vincent Cantin wrote:
> 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
 
Reply With Quote
 
xarax
Guest
Posts: n/a
 
      09-24-2004
"Vincent Cantin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> > > 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.

>
> 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.


 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      09-24-2004
xarax wrote:
> 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
 
Reply With Quote
 
xarax
Guest
Posts: n/a
 
      09-24-2004

"Chris Smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed).. .
> xarax wrote:
> > 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.


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


 
Reply With Quote
 
Vincent Cantin
Guest
Posts: n/a
 
      09-25-2004
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JNI problem with Garbage Collector ? bart59 Java 0 06-17-2004 07:34 AM
OutOfMemoryException Error: Garbage Collector doesn't release memory to OS Pyramis ASP .Net 0 01-25-2004 04:37 PM
DVD Verdict reviews: ALIEN: COLLECTOR'S EDITION, ALIEN3: COLLECTOR'S EDITION, and more! DVD Verdict DVD Video 0 12-15-2003 10:05 AM
Garbage collector problem Colt Java 9 11-18-2003 01:15 PM
Garbage Collector Debugging Rob Tillie ASP .Net 11 08-18-2003 10:39 PM



Advertisments