• 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

Garbage collection

 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given in Mind Q's mock exam..
How many objects are eligible for garbage collection once execution has reached the line labeled Line A?
String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";
String newestName = name;
name = null;
//Line A
a) 0
b) 1
c) 2
d) 3
e) 4
The ans is b.(1 object).
My ans was 'a'. I thought name, newname and newestname points to the same string in the literal pool and that GC is also concerened with Heap. Am I right? If not, which object is eligible for GC?
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think one object is eligible for garbage collection.

The object created in the second line of the code has no reference so that is eligible for garbage collection.
 
mrudul joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please clarify if mu understanding is wrong.
Thanks
 
mrudul joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could anybody tell me wheather i am right or wrong?
garbage collection always troubles me!
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mrudul,
I think u r absolutely right,.
The String object created in line 2 is eligible for GC.
Good luck,
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not so sure .
Why wouldnt the object "name" be not elegible for GC .
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any object will get eligible for garbage collection only when the last reference to the obejct is removed.
In this case eventhough reference name is made null, the other referene newestName still pointing to the object. So, the object that was pointed to name was not garbage collected.
Also, all the references will be stored in stack and all the objects that are created by new keyword will be place in heap. When you assign any new reference ( by creating new object), automatically the old reference will be made eligible for garbage collection.
I hope this would answer you question.....
 
Puneet Agarwal
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a small clarification i need . I guess it is the objects which are made avilable for GC and not references , right . When I say something like String str = "abc" , I am creating a new object right and a reference is getting assigned to it . Now if I say that str = "def" , then "abc" object will be avilable for GC , right...
 
Sripada Phani
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,
Puneet is very right. But let me add something to it (if I am not mistaken), String object is not mutable (read only). So when newName assigned to 'Jason', at line 3, it creates a new object and the newName was being referenced to it. Thus, the previous object, 'Nick', was made available for GC.
Hope my explanation is correct. If it is wrong, my apologies.
CH
 
Lakshmi Saradha
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all..
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well 2 objects will be eligible for Garbage Collection
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone!
I just want to clarify for everyone what's on the exam and what's NOT!
Understanding String immutability IS on the exam, and it's big!
Understanding when (non-String), objects are eligible for GC IS on the exam, and it's big and complicated!!!
However, because the String constant pool is a bit vague, the exam WILL NOT ask you to know when String objects are eligible for the GC.
So I'd recommend that you start this thread all over again using objects of a type other than String - otherwise this whole thread is confusing and misleading!
Bert
 
Lakshmi Saradha
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnak you Bert for letting us know the Exam objective.
 
CH Lee
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bert Bates:
Hello everyone!
I just want to clarify for everyone what's on the exam and what's NOT!
Understanding String immutability IS on the exam, and it's big!
Understanding when (non-String), objects are eligible for GC IS on the exam, and it's big and complicated!!!
However, because the String constant pool is a bit vague, the exam WILL NOT ask you to know when String objects are eligible for the GC.
So I'd recommend that you start this thread all over again using objects of a type other than String - otherwise this whole thread is confusing and misleading!
Bert


Hi Bert,
Just out of curiousity, could you tell us, at least me, how many object will be GC?
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
CH -
If you restate the question without using String objects, I'll give you my opinion
Bert
 
CH Lee
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bert Bates:
CH -
If you restate the question without using String objects, I'll give you my opinion
Bert


Hi Bert,
It's OK then. Just curious to know something out of the scope of SCJP exam.
Thanks.
Cheers.
 
The City calls upon her steadfast protectors. Now for 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