• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Garbage collection

 
Ranch Hand
Posts: 66
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ??
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
c2=null
 
Ranch Hand
Posts: 31
MyEclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 54
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 10
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Marshal
Posts: 74044
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You had your fun. Now it's time to go to jail. Thanks for your help tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic