• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question on Garbage Collection

 
Greenhorn
Posts: 18
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This Question is from Kathy Sierra page 269 chapter 3.






when //doStuff is reached,how many objects will create and how many objects will be eligible for GC?? Please give proper explation,if possible explain with the diagram...Please...
 
Ranch Hand
Posts: 59
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi mate,
Please use some code tags.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And please QuoteYourSources.

Also I'm sure I've seen that exact piece of code posted here before. If you search the forums for "CardBoard" there's a good chance you will find it.
 
Shanu Pandey
Greenhorn
Posts: 18
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am unable to understand from previous posts..Can you please explain it for me...please...
whether 3 objects will be created or 4?? explain how???
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to have to delete this post if you don't tell us the source of this question.
 
Shanu Pandey
Greenhorn
Posts: 18
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This Ques is from Katthy Sierra page 269 chapter 3.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
c1 is null ..this is very explicit.
c3 is also null since the object reference returned by the method 'go' is null;

C2 is pointing to a object on the heap. So C2 is not available for GC.
So only c1 and c3 are available for GC.

You may get confused as to how c2 is not null since it is passed as an argument to the method 'go' which sets it to null.

It must be understood that c2 is a reference to an object and not the actual object itself.
Also in java it is always pass by 'value' ..

So when pass c2 as the parameter to the 'go' method, the jvm creates a copy of 'c2' and passes it to the 'go' method.

c2 -> {some object on the heap }
jvm creates a copy of c2 ..and names it cb.
so cb is also pointing to the same object that c2 was pointing to..
inside the method you are doing 'cb = null ' which affects cb only and not c2 .
c2 continues to point to the same object as it was earlier.

so except for c2 other references are null which means they are not pointing to any object and hence eligible for garbage collection.




 
Shanu Pandey
Greenhorn
Posts: 18
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Yogesh Gnanapraksam,it was very helpful for me...thanks a lot....


Now i just want to know only one thing that,,will line CardBoard c3=null(generated by CardBoard c3=c1.go(c2)) will create any object in the heap or not? And you also have not given the ans that total how many objects will create in the heap.
So please reply..
 
Yogesh Gnanapraksam
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now i just want to know only one thing that,,will line CardBoard c3=null(generated by CardBoard c3=c1.go(c2)) will create any object in the heap or not?




Just think about what happens during runtime..

CardBoard c3 = cb; ( the reference returned by the go method) .
cb is not pointing to any object on the heap ...consequently c3 will also not point to any object on the heap.

Forget about object creation,for garbage collection you need to know whether the reference (c3 in this case) is pointing to any object on heap. In this case it is not ,so it is eligible for garbage collection.
You create objects by using the 'new' operator only.
In this code the new operator is used only twice..so only two Cardboard objects are created...

A simple way to check this is to have a static variable in your class and increment it in the constructor..


You should try running the program with System.out.println() at each point to know what is happening..


You are welcome..

Regards
Yogi



 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic