This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer (Exam 1Z0-830) Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide and have Khalid Mughal and Vasily Strelnikov on-line!
See this thread for details.
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Qn on GC

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class X2{
public X2 x;
public static void main(String args[]) {
X2 x2= new X2();
X3 x3 = new X3();
X2.x=x3;
X3.x=x2;
x2=new X2();
x3=x2; //line 9
doComplexstuff();
}
}

how many obj. will be eligible for garbage collection after line 9 runs??
ans is 2, can anybody explain??
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what is class X3? In your code sample it creates a reference of type X3, but no class definition for X3 exists.

Also, below the Add reply button are a set of buttons, one called CODE puts CODE tags around your code so that it keeps its formatting and indentaiton.

Mark
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shouldn't the answer be 1 instead of 2 cause it seems there is only 1 object eligible for garbage collection after line 9??

Correct me if I am wrong.
Thanks
 
cs singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no, the correct answer is 2.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the code went to docomplexstuff() after line 7, without having:

x2=new X2();
x3=x2; //line 9

the answer is 2..but after 9, both x2 and x3 shud refer to the same object. so, x2 and x3 shud be still on the heap. also, there is no code snippet showing/not showing if the instance var. are passed to some other method.

i think its 0...plz tell me how its 2
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please put up the correct code in proper format.
Thanks
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


both x2 and x3 shud refer to the same object. so, x2 and x3 shud be still on the heap


You are right. But a small correction, x2 and x3 are not objects, they are references to object. In this case after line 9, references x2 and x3 are pointing to the same object only.
The objects that are eligible for garbage collection are those created at line 4 and 5.(hence the answer 2)

Regards
Lakshmanan
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even i feel Lakshman is rite, if the corrected code ia as below

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There will be 2 objects eligible for garbage collection.
There are two objects that get created by following lines:

X2 x2= new X2();
X3 x3 = new X3();

when a new object is assigned to both x2 and x3 references, the above mentioned objects are redundant and eligible for garbage collection.
Let me know if this helps.
reply
    Bookmark Topic Watch Topic
  • New Topic