• 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 Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

objects eligible for GC?

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
source: https://coderanch.com/t/268621/java-programmer-SCJP/certification/many-objects-eligible-garbage-collection
How many objects are eligible for garbage collection after line 1.

class test
{
public static void main(String args[])
{
test t = new test();
String[] s=t.f(); //line 1
System.gc();
}
public String[] f()
{
String[] s = new String[4];
for(int i=0;i<s.length;i++)
{
s[i]=new String("" +i);
}
String[] s2 = new String[2];
s2[0]=s[0];
s2[1]=s[1];
return s2;
}
public void finalize()
{
System.out.println("ggg");
}
}



My answer is 5 objects.

object reference by s is local var in f().so scope of this variable with in the method only.when method execution is completed it is removed.
if s is unreferenced then elements in s are also eligible for GC.
totally 4 objects elgible for GC.

correct me if i am wrong.......

 
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think 3 objects will be eligible for GC

s[]
s[2]
s[3]
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think 4?

s[]
s2[]
s[2]
s[3]
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ankit
array is an object and it has 4 elements which is pointing 4 objects.
if s[] is eligible for GC then what is the status of the elements s[0],s[1],s[2],s[3] which s[] contains?

 
Ankit Garg
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still think that 3 objects are eligible

s[0] and s[1] are not eligible because they are assigned to s2[0] and s2[1] respectively

s2[] will not be eligible because the array to which it is pointing is returned to main so s[] in main will receive it...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic