• 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

How many objects are eligible for garbage collection

 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many objects are eligible for garbage collection after line 1.


code is:

-----------------------------
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 3 objects.

object reference by s
object referece by s[2]
object reference by s[3]

Is it correct?
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes it is. those objects are created in f() method, and not referenced anywhere else so they can be GCed.
please use code tags!!!
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My answer is 2 objects.
object referenced by S is still valid at System.gc(). It will be unreachable only after main method is over.
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not getting what are you people saying. can you please elaborate
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dinesh Tahiliani:
not getting what are you people saying. can you please elaborate



What don't you understand?
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thes answers
object reference by s
object referece by s[2]
object reference by s[3]
 
Vishal Matere
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dinesh Tahiliani:
thes answers
object reference by s
object referece by s[2]
object reference by s[3]



Although I dont agree with these answer I can atleast tell you what they meant.
S variable which is defined in main method
s[2], s[3] are objects in array defined in method F().

Are you still not sure what they meant?

V
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for that explaination, I want to understand the answers, how it came.
 
Vishal Matere
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dinesh Tahiliani:
Thanks for that explaination, I want to understand the answers, how it came.



s[2] & s[3] objects in f() method were NOT returned at the end of function f() hence I feel that they are eligible for Garbage collection.

On the other hand, s in main() is still referenced until end of main() method.
Hence only 2 objects for garage collection at line 1

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

i think object defined on line 2(String array) is also eligible for gc because it is locally defined object, it doesn't have to do anything with s that is local to main method.
 
Vishal Matere
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry I could be wrong but

will not create any object as such. It just creates a variable reference of capacity 4.

I guess we need to run this program once to see actual results, do we?

V
 
Ivan Ivanic
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whenever you see new keyword there is object created. arrays are objects. array contains references to objects, those references can point to null(default) or can point to some objects, if you assign them objects.
in line 2 one object is created --> array that holds String refs
in line 3 in every successive execution of for loop you are assigning one new String object to refs in array
 
Vishal Matere
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ivan,
I tried following code with little alteration to original code (for debug purpose)


Output came out to be

Contructor 445268406
Contructor 461046198
Contructor 460145078
Contructor 459702710
Finalizing 460145078
Finalizing 459702710


This means that only 4 objects got created , that also inside for loop and
only 2 objects for finalised(garbage collected).

V
 
Ivan Ivanic
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes only four objects of type StringS. but arrays are also objects.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As always, the real exam will NOT use objects of type String for GC related questions - the String constant pool makes that too confusing.

When you see a GC mock question that uses objects of type String - just insert another type - hmmm, better stay away from wrapper types too!
 
Ivan Ivanic
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


ok, here we go. on line 2 we created an array which is an object. this array holds references for Dog objects.
on line 3 in every succession of for loop we have assigned Dog object to refs in array.
on line 4 we created one more array object and assigned it to s2.
on line 5 and line six we assign existing Dog objects to refs in array s2
on line 7 we return s2
on line 1 we assign object refered by s2 to s.

after line 6 we have:
two array objects referred by s and s2.
four Dog objects:
first referred by s[0] and s2[0]
second referred by s[1] and s2[1]
third referred by s[2]
fourth referred by s[3]
so here we have 6 objects that are local to method f()

when s2 is returned in line 7 and assigned to s in line 1, and f() method is finished.
we have three objects from f():s2, s2[0] and s2[1]; that are still referred by s in main method so they are no eligible for gc.
and we have three objects from f():s, s[2] and s[3]; that are no longer referred anywhere so they are eligible for gc.
so my final answer is:
there are three objects eligible for gc after line 1
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
very well explained
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ivan, Brillian Explanation..

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

and we have three objects from f():s, s[2] and s[3]; that are no longer referred anywhere so they are eligible for gc.




If the array object referred by s is eligible for gc how come the elements in that array object (s[0] and s[1]) are not eligible for gc? You know what i'm saying? Therefore i think only s[2] and s[3] are eligible for gc and not the array object referred by s. Since the array object referred by s contains elements that are still referred by s2[0] and s2[1] and the array object that contains them referred by s2 is referred by s in the main method.
[ May 09, 2008: Message edited by: sridhar row ]
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
while everthing is referenced:

Reference variables
s: referring Object 1
s2: referring Object 2

Arrays
Object 1: referring Object 3,4,5,6
Object 2: referring Object 3,4

Strings
Object 3: referring "1"
Object 4: referring "2"
Object 5: referring "3"
Object 6: referring "4"

if "s" is set to null it looks that way:

Reference variables
s: null
s2: referring Object 2

Arrays
(eligable for gc) Object 1: referring Object 3,4,5,6 <- dead object / no reference
Object 2: referring Object 3,4

Strings
Object 3: referring "1"
Object 4: referring "2"
(eligable for gc) Object 5: referring "3" <- dead object / no reference
(eligable for gc) Object 6: referring "4" <- dead object / no reference


Final state:

Reference variables
s: null
s2: referring Object 2

Arrays
Object 2: referring Object 3,4

Strings
Object 3: referring "1"
Object 4: referring "2"
[ May 09, 2008: Message edited by: Frank Hinkel ]
 
The knights of nee want a shrubbery. And a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic