This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

GarbageCollection

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

this question is from whizlabs mock exam

public class Test
{
public static void main(String[]args)
{
Test t=new Test();
String[]s=t.f();//Line 1
System.gc();
Thread.sleep(5000);
}
public String[] f()
{
String[]s=new String[4];
for(int i=0;i<s.length;i++)
s[i]=new String(""+i);
String []s1=new String[2];
s1[0]=s[0];
s1[1]=s[1];
return s1;
}
}
How many Objects are eligible for GrabageCollection after Line 1

Answers
A) 0
B) 1
C) 2
D) 3
E) 4
F)5

Answer is D, my doubt is why option C is not correct
and anyone please suggest me how to slove grabage coolection questions
in exam, iam getting Confused
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by gobburi saikrishna:
Hello Ranchers

this question is from whizlabs mock exam

public class Test
{
public static void main(String[]args)
{
Test t=new Test(); //first object 't' will be created
String[]s=t.f();//Line 1
System.gc();
Thread.sleep(5000);
}
public String[] f()
{
String[]s=new String[4]; //second object s[]'string of arrays' is created
for(int i=0;i<s.length;i++)//**third,fourth,fifth,sixth are string objects s[0],s[1],s[2],s[3]**/
s[i]=new String(""+i);
String []s1=new String[2]; //seventh object is s[] another string of array
s1[0]=s[0]; //no new objects are created(refer to s[0])
s1[1]=s[1]; //no new objects are created(refer to s[1])
return s1;
}
}

So far seven objects are created,in which after the completion of the method we don't need seconds[],fifth(s[2]),sixth(s[3]..so 3 objets are available for garbage collection..

hello ranchers,whether my approach towards the gc() question is correct becoause i too feeling GC q's difficult to solve.


How many Objects are eligible for GrabageCollection after Line 1

Answers
A) 0
B) 1
C) 2
D) 3
E) 4
F)5

Answer is D, my doubt is why option C is not correct
and anyone please suggest me how to slove grabage coolection questions
in exam, iam getting Confused

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah I am confused too..

As mentioned there are 7 objects created.
So if two objects were assigned null,wouldnt 2 objcts be eligible for Gc
 
m prabhu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
case 1:
---------
A a = new A();
a = null;
System.gc();

in this case one object 'a' is available for GC

case 2:
------
A a = null;
System.gc();
in this case there is no object created itself;hence there is no object available for GC
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Prabhu, 3 objects will be eligible for GC. These will be the String array object that was referenced by s and the two strings objects s[2] and s[3].
[ March 24, 2008: Message edited by: pranav bhatt ]
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Ranchers,

A Picture is worth than 1000 lines of text. Here you go for the detailed explanation.


For the Question




For the Objects inside the method f1()





For the Objects inside the method main()





The Answer is [b]3[b] only. There are three objects eligible for GC.

Cheers
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks 4 Raghavan Muthu`s answer.
 
author
Posts: 9034
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will be a LOT easier for you if you don't use objects of type String in GC questions. Because of the String constant pool there are NO GC questions on the real exam that use objects of type String.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That makes life a little easy
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's very true. Thank you Bert. You keep reminding the people in the right time.

Infact, this becomes tricky when involves Strings as "string literals are never GCed bcos of string literal pool"
 
Onion rings are vegetable donuts. Taste this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic