aspose file tools*
The moose likes Beginning Java and the fly likes Garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Garbage collection" Watch "Garbage collection" New topic
Author

Garbage collection

archu sweet
Ranch Hand

Joined: Mar 07, 2011
Posts: 66


When // doStuff is reached,

I guess 3 objects are eligible for Garbage collection..C1,C2,C3...am i correct?? correct me if i'm wrong ??
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14350
    
  22

Can you explain why you think that there are 3 object eligible for garbage collection?

Hint: What do you think c2 refers to when you reach the line with // doStuff?

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
archu sweet
Ranch Hand

Joined: Mar 07, 2011
Posts: 66

c2=null
Yunnan Zhou
Ranch Hand

Joined: May 04, 2011
Posts: 31

I don't think so . GC influence efficiency, you just hava 3 objects. GC will no be started.
when you set a object to null, you just tell GC that it can be recovered . but you don't know when the GC will start.it's uncertain.
Generally speaking,when you memory is not enough,GC will start.
you can difine a method like finalize() to monitor the GC.


I'am a Chinese.I like Java.Hello.
Jared Malcolm
Ranch Hand

Joined: May 02, 2011
Posts: 54

Yunnan Zhou wrote:GC will no be started.


As you mention in the rest of your quote...you have no way of knowing that GC hasn't started... I also don't think he's literally wondering if it's been GC'd just if it can be....(could be wrong here).


SCJA 6 (Studying for SCJP 6)
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

Yunnan Zhou wrote:...when you set a object to null, you just tell GC that it can be recovered . but you don't know when the GC will start.it's uncertain.
Generally speaking,when you memory is not enough,GC will start...

Yes, correct. This kind of questions are more of SCJP question types which ask the *eligibility* for GC incase it's started by the JVM...


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Peeyush Bhadola
Greenhorn

Joined: Jan 23, 2011
Posts: 10

When // doStuff is reached, I think things will be seen in this way:

At line#10: c3 = null. So there is no object present to be marked for GC.

At line#11: c1 = null. That means the remote control(reference) c1 is not pointing to any object and hence the object created at line#8 can be marked for GC.

At line#9: The remote control(reference) c2 is still controlling the object. So there is no point marking this object for GC.

This is from my understanding. So if you find any mistake in my answer, please help. Thanks
Jared Malcolm
Ranch Hand

Joined: May 02, 2011
Posts: 54

Peeyush Bhadola wrote:At line#10: c3 = null. So there is no object present to be marked for GC.


I think you may misunderstand GC....if an object no longer has a reference pointing to it...that's the point at which it is ELIGIBLE to become GC'd (i.e. object = null == ready to be garbage collected). An object = null is still consuming memory until the GC runs....this is due to the fact that the Object still exists it just no longer has a pointer to it to be able to be accessed!

As for the solution...just run it and find out!

Peeyush Bhadola
Greenhorn

Joined: Jan 23, 2011
Posts: 10

Jared Malcolm wrote:I think you may misunderstand GC.

Hi Jared thanks for the explanation but I think you misunderstood me. What I was saying is,
At line#10, c1.go(c2) returns null and that means reference c3 has nothing to point. Therefore I wrote
Peeyush Bhadola wrote:At line#10: c3 = null. So there is no object present to be marked for GC.

Also, I am very much aware of the fact what you have mentioned:
Jared Malcolm wrote:if an object no longer has a reference pointing to it...that's the point at which it is ELIGIBLE to become GC'd

... and that is why i wrote :
Peeyush Bhadola wrote:At line#11: c1 = null. That means the remote control(reference) c1 is not pointing to any object and hence the object created at line#8 can be marked for GC.

And in case of reference c2, it is still pointing the object present in memory. So it can't be marked for GC.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14350
    
  22

archu sweet wrote:c2=null

Where do you think c2 is set to null?
archu sweet
Ranch Hand

Joined: Mar 07, 2011
Posts: 66

@ jesper:

CardBoard c3 = c1.go(c2); i when this line is compiled go function is invoked which sets c2's value to null in go function...am i correct??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
archu sweet wrote:. . . ..am i correct??
No.

Java™ is entirely pass-by-value; it is not possible for the method to alter the value of c2. There is lots more discussion in this thread.
 
 
subject: Garbage collection