• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How many are objects are eligible for GC?

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source:http://www.techinterviews.com/?p=281

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


Iam thought my answer is 1.And this is how I came to that conclusion

1) First a String ref variable name is created.And it is not referring to any String obj.

2)Another String ref variable newname is created and referring to the object NICK

3)Now newname is referring to object Jason. so Nick is eligible now for GC

4)name is referring to "FRIEDA" and newestname also referring to same object.(So here 2 reference variable name and newestname are referring to same object FRIEDA.)

5)name is set to null.

On the whole 1 object only eligible for GC,which is in step 3.

Am I correct?If not please let me know where I had gone wrong.

Actually a bit confused with step 5 also.As name is set to null,is this also eligible for GC? I feel name is a string ref variable only not a string object,so it is not eligible.

If I sound ridiculuous in my doubts Iam really sorry.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read this post
 
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
The link you have given seems to have gone a long way Sheelpa!

I guess it is too much they discussed there
 
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

Originally posted by vatsalya rao:
...If I sound ridiculuous in my doubts Iam really sorry.



No issues Vatsalya. That's why this forum is for and we are all learning by discussing only

Let me take a moment to clarify.

Kindly make use of Code Tags whenever you paste the java code so that it will look uncluttered.

Before we discuss about this specific question, lets see a small example.



As such, we do have 2 reference variables named s1 and s2 of type java.lang.String. There are two string constants whose value equals "Sample" and "String".

As the term indicates "String Constants" are being evaluated at the compile time and NOT at the runtime. The String constants are created when the class is loaded and they do reside in a special area called String Literal Pool and hence they do not belong to the traditional Garbage Collectible Heap!

If you are clear with this, there is one more thing we need to see here.

The second line in teh above example creates a brand new String because of the new operator, as the JVM is instructed to oblige you to do so until and unless you intern the string by invoking the intern() method on the particular string reference.

As a conclusion, here only ONE string object is created as a result of executing the line 2 and because of the new operator. As such, here the newly created String object refers to the String "String" which is already created and referenced from the String Literal Pool.

One special note is that the constants in the String Literal Pool are never eligible for Garbage Collection as they don't belong to the Garbage Collectible Heap. They always have a reference to them until the JVM shuts down or the java.lang.String class is unloaded.

Now, lets look at your example.



We can infer a few things from the code as per our sample program with 2 lines earlier.

  • During the class loading operations, the following String constants are created and put in String Literal Pool. "Nick", "Jason", and "Frieda".


  • Please keep in mind that, they are NOT the objects of Garbage Collectible Heap, instead those are the constants part of String Literal Pool.
  • As such,there are NO new garbage collectible objects created at all. all what you deal is the aforesaid string constants.


  • So setting the string reference variable here will have no effect.

    The available options are:



    Since, no object is getting created in the Garbage Collectible Heap, no object is eligible for GC.

    Thus, the answer is a) 0.

    Hope this helps!
     
    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
    Please spend sometime on reading this excellent article by Corey here Strings Literally.

    You will get to know more
     
    author
    Posts: 9031
    21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The key point in the long thread that was linked to here is the point that Jim made:

    On the real exam, there will be NO questions asking about how objects of type String are garbage collected. The GC questions will always use objects of other types.
     
    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
    That's very true Bert!

    Thank you
     
    pie. tiny ad:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic