File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage collection

 
archu sweet
Ranch Hand
Posts: 66
Android Chrome Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
archu sweet
Ranch Hand
Posts: 66
Android Chrome Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
c2=null
 
Yunnan Zhou
Ranch Hand
Posts: 31
C++ Java MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jared Malcolm
Ranch Hand
Posts: 54
Java MySQL Database PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Vijitha Kumara
Bartender
Posts: 3909
9
Chrome Fedora Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Peeyush Bhadola
Greenhorn
Posts: 10
Chrome Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
Java MySQL Database PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
Chrome Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
archu sweet wrote:c2=null

Where do you think c2 is set to null?
 
archu sweet
Ranch Hand
Posts: 66
Android Chrome Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic