• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Garbage collection question in K&B

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Im trying to understand this question, well its answers, as I dont know why my original answer is wrong, take a look:



When the code reaches line 6, which are eligible for garbage collection? (Choose all that apply.)
A. st
B. in
C. i3
D. The object created on line 5.
E. The object created on line 9.
F. The object created on line 10.
G. The object created on line 11.

Answer (for Objective 7.4):
D, E, and F are correct.
A, B, and C are incorrect because only objects are considered by the GC.

Im just curious as to why in and i3 are not considered as Objects being valid for garbage collection. Is it that they are reference variables pointing to Objects? And if so, is it that when I take the exam, and see any similar questions, I dont consider the actually reference variable at all per se.? Any help greatly appreciated.
 
Greenhorn
Posts: 29
Mac Java ME Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Ben ,
let analyse the method go

void go() {
this.MyInner in = this.new MyInner(); // that mean Toolbox.MyInner in = Toolbox.new MyInner();
Integer i3 = in.doInner();
Toolbox t = new Toolbox();
st = t;
System.out.println(i3);
14. }



so the Object

this. new MyInner()

still have reference that object so for that it cant be GC.
and only D, E, and F are correct.
you can see other examplejavaranch
 
Sheriff
Posts: 28371
99
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

Ben Synes wrote:Im just curious as to why in and i3 are not considered as Objects being valid for garbage collection. Is it that they are reference variables pointing to Objects?



Yes. Don't confuse variables and objects; garbage collection only collects the objects, although when it does so it has to take into account whether there are variables which refer to the objects.

And if so, is it that when I take the exam, and see any similar questions, I dont consider the actually reference variable at all per se.? Any help greatly appreciated.



I don't know about the exam, but in general when you're trying to figure out what objects are available for garbage collection it can be confusing if you refer to an object by the name of a variable which refers to that object. It gets especially confusing when the variable is changed to refer to some other object. So when you see "A thing = new B()" try not to call that B object "thing".
 
He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic