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-23-2004
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 ?


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

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

It doesn't answer my question.


 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      09-23-2004
On Thu, 23 Sep 2004 19:42:08 +0800, Vincent Cantin wrote:
>> > 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.
>
> 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

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
xarax
Guest
Posts: n/a
 
      09-23-2004
"Gordon Beaton" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Thu, 23 Sep 2004 19:42:08 +0800, Vincent Cantin wrote:
> >> > 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.
> >
> > 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.


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


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




 
Reply With Quote
 
Vincent Cantin
Guest
Posts: n/a
 
      09-24-2004
> > 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
> ((E-Mail Removed))


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


 
Reply With Quote
 
Ann
Guest
Posts: n/a
 
      09-24-2004

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

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


 
Reply With Quote
 
Chris Smith
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.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
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.


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 ?


 
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