Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection Question

 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came across this snippet from whizlabs mock test. I have modified the code for my understanding.


In line 1, we have set the Exam instance to null. We have an Exam instance and its associated, String myString, Float myFloat, all these(3) are eligible for GC. is this right.

confused about the Instance Variable Integer myInteger and Exam e1, it has not been initialized, but it will have a default null value.I think it will it be eligible for GC.

Integer code is pointing to where Integer c points, so it is not eligible for GC.

int myPrim is a primitive..is that why it is not considered for GC?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:In line 1, we have set the Exam instance to null. We have an Exam instance and its associated, String myString, Float myFloat, all these(3) are eligible for GC. is this right.

confused about the Instance Variable Integer myInteger and Exam e1, it has not been initialized, but it will have a default null value.I think it will it be eligible for GC.

Integer code is pointing to where Integer c points, so it is not eligible for GC.

int myPrim is a primitive..is that why it is not considered for GC?

Let's reiterate quickly some rules related with Garbage Collection. Let's start with the most important one: only objects can be eligible for GC! That means that reference variables, primitive variables and primitive values will never be eligible for GC. So a useful tip: if you count the occurences of the new operator, you have an idea of how many objects at most can be eligible for GC. But be careful: some objects can be created without using the new operator (I'll have a pop quiz question about this later on).
And the second rule: when an object can not be reached anymore by an active reference variable, the object becomes eligible for GC.

So let's have a look at the main method. On the first line of this method, an Integer object is created (using autoboxing) and is assigned to reference variable c (number of objects = 1). Then a new Exam object is created (number of objects = 2). This instance has a bunch of instance variables and some of them refer to an object: myString refers to String literal "myString" (number of objects = 3), code refers to the same object c is referring to (so the number of objects remains 3), myFloat refers to a Float object with value 3.14f (number of objects = 4), and myPrim has the value 10 (which is a primitive value and therefore not an object). All other instance variables (myInteger and e1) don't refer to an object (and have the value null), therefore the number of objects doesn't change.
After line1 is executed, the Exam object is "lost" (can't be referred anymore by an active reference variable) and therefore becomes eligible for GC. But the object itself contained also reference variables which might refer to other objects as well. If these objects can't be referenced anymore with an active reference, these objects will become eligible for GC as well. So let's have a look: the String literal "myString" is (as you should know) still referenced from the String Literal Pool and therefore is not eligible for GC. The object referenced by code is also referenced by the reference variable c which is still active and therefore the Integer object is nog eligible for GC. The Float object (with value 3.14f) is "lost" as well (can't be referred anymore by an active reference variable) and thus is also eligible for GC. So in total 2 objects will become eligible for GC after line1 is executed.


And here is the pop quiz question I've promised Which objects can be created without using the new keyword? (tip: you are looking for 4-5 common cases and I already give one for free in the previous explanation).

Hope it helps!
Kind regards,
Roel
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will remember all the rules.

the String literal "myString" is (as you should know) still referenced from the String Literal Pool and therefore is not eligible for GC.


Yes, I have read that in the SCJP tip line. "Unlike most objects, String literals always have a reference to them from the String Literal Pool. That means that they always have a reference to them and are, therefore, not eligible for garbage collection."..So if myString was something like this, then it would have been eligible for garbage collection.

But in the mock test explanation it is given "making w reference null will cause the Exam instance eligible for for GC, and also the string reference too." ..this seems wrong.

Which objects can be created without using the new keyword


I think the answer is the objects created using autoboxing.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:So if myString was something like this, then it would have been eligible for garbage collection.

You are spot-on!

Ramya Subraamanian wrote:But in the mock test explanation it is given "making w reference null will cause the Exam instance eligible for for GC, and also the string reference too." ..this seems wrong.

That's wrong for two reasons:
1/ a reference is never eligible for GC, only objects can be eligible for GC!
2/ the String object "myString" is clearly a String literal and therefore it's also referenced from the String Literal Pool. So although w is set to null, this String object is still referenced by an active reference (namely the String Literal Pool) and thus this String object is not eligible for GC.

Ramya Subraamanian wrote:I think the answer is the objects created using autoboxing.

Using autoboxing you can indeed create instances of primitive wrapper classes without using the new operator. But that's just 1 case (out of a possible 4-5) and this one was already mentioned in my previous post. Do you know any other cases? (Another one is also mentioned in my previous post )

Ramya Subraamanian wrote:I will remember all the rules.

When objects become eligible for Garbage Collection is one of the most popular (and sometimes hard to understand) topics in this forum, so if you use the search function you'll find plenty of (excellent) topics about this topic. Here are a few:
  • How many objects eligible for garbage collection? (K&B7, chapter 3, self test question 11)
  • Chapter 3 Of OCA/OCP Java SE 7 Programmer I & II Study Guide, need help on question 11
  • Doubt about CH3 Q11: how many objects eligible for garbage collection? (K&B7)
  • doubt on question #6 self test chapter 5 book and cd [K&B7]


  • And finally, although strings and garbage collection can be lots of fun I like to mention this quite important note (from K&B7, page 201)
    K&B7 wrote:Note: Due to the vagaries of the String constant pool, the exam focuses its garbage collection questions on non-String objects, and so our garbage collection discussions apply to only non-String objects too.


    Hope it helps!
    Kind regards,
    Roel
     
    Ramya Subraamanian
    Ranch Hand
    Posts: 178
    17
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    When objects become eligible for Garbage Collection is one of the most popular (and sometimes hard to understand) topics in this forum, so if you use the search function you'll find plenty of (excellent) topics about this topic. Here are a few:


    I will go through the posts. Yeah, I find this pretty hard to understand. Thank you Roel.

    K&B7 wrote:
    Note: Due to the vagaries of the String constant pool, the exam focuses its garbage collection questions on non-String objects, and so our garbage collection discussions apply to only non-String objects too.


    So we can expect no questions on GC with String !!!

    But that's just 1 case (out of a possible 4-5) and this one was already mentioned in my previous post. Do you know any other cases? (Another one is also mentioned in my previous post )


    Okay..I think I missed String created without new operator . Then Array is a container Object..there's this declaration with braces where we can initialize it.And we can convert that to List with asList. Create a copy of the Array with copyof .Create a clone of that array...then I think enum constants are also objects. I guess.. there would be many more.



    }
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ramya Subraamanian wrote:So we can expect no questions on GC with String !!!

    Indeed!

    Ramya Subraamanian wrote:Okay..I think I missed String created without new operator . Then Array is a container Object..there's this declaration with braces where we can initialize it.And we can convert that to List with asList. Create a copy of the Array with copyof .Create a clone of that array...then I think enum constants are also objects. I guess.. there would be many more.

    You are spot-on Have a cow for providing such extensive list (and thus answering the question correctly)! In this post you'll find a nice overview (with code examples) about some cases where you don't need the new keyword to create a new object/instance.

    And here is a final question: how many objects will be eligible for GC after line5 is executed?
     
    Ramya Subraamanian
    Ranch Hand
    Posts: 178
    17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I thought, Array objects of wrapper type Short and Double are set to null, so they are eligible for GC. But instanceof thinks they are not Objects, not sure how !!.so because they are not objects, they are not eligible for GC.

    There is an object created at line 3. o2 refers to the object created by o. So even though o is set to null, there is still a to the reference to that object through o2. So it is not eligible for GC.

    So no objects eligible for GC !
     
    Paul Clapham
    Sheriff
    Posts: 21581
    33
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I don't see how you can say that those array objects are not objects. I mean, think about it... an object which isn't an object? Fortunately you already have an inkling that there's something wrong with that idea.

    Which means that your concept of "instanceof" must be flawed in some way. But I find it hard to tell how, since I don't think I can see the code which you're confused by. Could you post a small fragment which illustrates your confusion?
     
    Ramya Subraamanian
    Ranch Hand
    Posts: 178
    17
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    okay, I made a mistake.. I used instanceof after the array objects were set to null. so it returned false. I was wondering all this while..


    So ..I correct my answer..2 objects eligible for GC. sorry about the unncessary confusion.
     
    Paul Clapham
    Sheriff
    Posts: 21581
    33
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ah, so that's it! Yes, null is not an instance of Object and so... well, you know the rest already.
     
    salvin francis
    Bartender
    Posts: 1339
    11
    Eclipse IDE Google Web Toolkit Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Roel De Nijs wrote:But be careful: some objects can be created without using the new operator (I'll have a pop quiz question about this later on).

    I would love to be a part of that quiz ...
    For now, out of curiosity.. only a 3 things come to my mind over objects created without new:

  • Strings : String str = "hello";
  • Autoboxing: Integer number = 12;
  • I think I had once read about a bridge created in generics in effective java book, I dont recall it exactly now


  • Am I missing something in my tiny list ?
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    salvin francis wrote:I would love to be a part of that quiz ...

    No problem! The more the merrier (although the answers are already given in previous posts)

    salvin francis wrote:
  • Strings : String str = "hello";
  • Autoboxing: Integer number = 12;

  • Those two should definitely be on your list!

    salvin francis wrote:Am I missing something in my tiny list ?

    You definitely are. At least 2 very common cases you are currently missing
     
    Ramya Subraamanian
    Ranch Hand
    Posts: 178
    17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, null is not an instance of Object


    I had this thought..if null is not an instanceof anything ..like Array, Object, String.then how can we pass null into methods which take these types.


    I found this from the JLS about null. http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.1

    There is also a special null type, the type of the expression null (§3.10.7, §15.8.1), which has no name.

    Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type.

    The null reference is the only possible value of an expression of null type.

    The null reference can always be assigned or cast to any reference type (§5.2, §5.3, §5.5).


    So can we conclude that.. Null is not an Object, its a reference type that can be assigned or cast to any
    reference type. and thats why are able to pass null into these methods.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ramya Subraamanian wrote:
    Yes, null is not an instance of Object

    I had this thought..if null is not an instanceof anything ..like Array, Object, String.then how can we pass null into methods which take these types.

    Because null is a special reference value. It can (only) be assigned to reference variables (as you've already read in the JLS). And it means the reference variable isn't referring to any object at all.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ramya Subraamanian wrote:sorry about the unncessary confusion.

    No need to apologize! We are all here to learn and most of the times you learn much more from mistakes (failure) than from success

    Ramya Subraamanian wrote:So ..I correct my answer..2 objects eligible for GC

    Your code snippet illustrates one important thing: every array IS-A Object (even an array with primitive values IS-A Object). So you are absolutely correct about the arrays. And you are also spot-on about the Object object. But your answer ("2 objects eligible for GC") is incorrect! Have another close look at the code snippet, maybe you see a bunch of other objects which might be eligible for GC
     
    Ramya Subraamanian
    Ranch Hand
    Posts: 178
    17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No need to apologize! We are all here to learn and most of the times you learn much more from mistakes (failure) than from success


    you are very generous ..thank you !

    Have another close look at the code snippet, maybe you see a bunch of other objects which might be eligible for GC




    Yeah,there are these values inside the Short and Double array na. They are autoboxed and stored inside the Array. So 2 wrapper Array objects and 5 wrapper objects inside them ...makes 7 objects eligible for GC.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ramya Subraamanian wrote:Yeah,there are these values inside the Short and Double array na. They are autoboxed and stored inside the Array. So 2 wrapper Array objects and 5 wrapper objects inside them ...makes 7 objects eligible for GC.

    You are getting closer to the correct answer, but your answer is still incorrect! I'll give an additional tip: not all array elements are eligible for GC...
     
    Ramya Subraamanian
    Ranch Hand
    Posts: 178
    17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I remember that like Integer, Short also uses caching mechanism. Double does not do caching.Short is cached if they are created using autoboxing and the value is between -128 and 127.

    In our Short array



    sa[1] which contains -100 is within the range -128 to 127 and it is cached.
    sa[0] which contains -1000 , will not be cached.

    Cache is used to store frequently re-referenced values. I think objects which are cached will not be eligible for GC.

    so -100 will not be eligible for GC. And -1000 is eligible for GC.

    2 Array Objects+ 1 Object from Short array+ 3 Objects from Double Array = 6 Objects eligible for GC.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ramya Subraamanian wrote:I remember that like Integer, Short also uses caching mechanism. Double does not do caching.Short is cached if they are created using autoboxing and the value is between -128 and 127.

    That's indeed true! More detailed information can be found here.

    Ramya Subraamanian wrote:Cache is used to store frequently re-referenced values. I think objects which are cached will not be eligible for GC.

    so -100 will not be eligible for GC. And -1000 is eligible for GC.

    Spot-on! A cached value will still be referenced by an active reference (namely the cache of autoboxed values) and therefore is not eligible for GC.

    Ramya Subraamanian wrote:2 Array Objects+ 1 Object from Short array+ 3 Objects from Double Array = 6 Objects eligible for GC.

    That's indeed the correct answer! Please note that on the exam you only get one attempt So you definitely have to apply everything you know on the first attempt


    Let's have another pop quiz question to test your knowledge. And the question is still the same: how many objects will be eligible for GC after line5 is executed

    Hope it helps!
    Kind regards,
    Roel
     
    Ramya Subraamanian
    Ranch Hand
    Posts: 178
    17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The int Array is an array of primitives. So it is only one object.(object 1)

    List Object created using list (object 2)

    list[0] - new String("Java") - A String created with new, hence eligible for GC(object 3)
    list[1] - Array object a, eligible for GC.(object 4)
    list[2] - new Integer(10) - A Integer object created with new , so no caching.eligible for GC(object 5)
    list[3] - 1000 - It is autoboxed but not within the range -128 and 127 , so no caching and eligible for GC(object 6)
    String Builder object -sb which is created which points to list[0] (object 7)

    so now at line 5, a is null and list is null and all objects inside it are also set to null.

    list[0] - has a reference to StringBuilder sb so will not be GCed.
    int i refers to a[2], but this will not have effect on GC of Array object a because it is a array of int primitives. And also it doesnt affect the array in the list[1].

    Now comes the issue with creating the list with asList. There are 2 ways(or more) to create a list using asList.
    asList is created using a new operator like in line 10... which is not like fixed or backed by an array and so even if we remove the element from index 1, it doesnt throw an unsupportedOperationException in line 11. But whereas in line 12 we create a list which is fixed size and backed by array, so when we remove an element it throws the exception in line 13.


    But now our List is created from asList, which returns a fixed-size list backed by the specified array. changes to the list affects the array. That means there is maybe a reference to that backed array from the list. So even when the list is set to null , there might be reference from that backed array, hence the list and its objects are not garbage collected ...This is my theory.

    But if this is true then..only one 1 int Array Object is eligible for GC
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You have written an extensive reply with some comments, remarks and assumptions. Some of them are correct, others are not. So let's review all of them. Fasten your seatbelt, here we go!

    Ramya Subraamanian wrote:The int Array is an array of primitives. So it is only one object.(object 1)

    List Object created using list (object 2)

    Both are correct! So currently the object count = 2.

    Ramya Subraamanian wrote:list[0] - new String("Java") - A String created with new, hence eligible for GC(object 3)

    You are absolutely spot-on about this one

    Ramya Subraamanian wrote:list[1] - Array object a, eligible for GC.(object 4)

    But this one is incorrect! a is a reference variable, not an object. So no object is created here and thus the object count remains 3.

    Ramya Subraamanian wrote:list[2] - new Integer(10) - A Integer object created with new , so no caching.eligible for GC(object 5)
    list[3] - 1000 - It is autoboxed but not within the range -128 and 127 , so no caching and eligible for GC(object 6)

    And here you are again 100% correct! Only the object count is wrong (due to your mistake about list[1]). So object count after list[2] is 4 and after list[2] the object count is 5.

    Ramya Subraamanian wrote:String Builder object -sb which is created which points to list[0] (object 7)

    It's true that a new StringBuilder object is created, so object count becomes 6. But sb does not refer (point) to list[0]. That's actually impossible! Because the type of sb is StringBuilder and the type of list[0] is Object. That line of code will create a reference variable sb which refers to a new StringBuilder object and the initial value of this new object will be equal to "Java" (the String object referred by list[0]).

    Ramya Subraamanian wrote:so now at line 5, a is null and list is null and all objects inside it are also set to null.

    This is incorrect! When for example an array reference variable or a List reference variable is set to [tt]null
    , nothing happens with the objects inside so they are definitely not set to null. But objects which are referenced from the array (or list) and which do not have any other active reference will be eligible for GC.

    Ramya Subraamanian wrote:list[0] - has a reference to StringBuilder sb so will not be GCed.

    This is incorrect too! First of all, list[0] does not have a reference to sb. As explained earlier list[0] is only used to create a new StringBuilder object with the same value as the String object "Java" (referred by list[0]). Secondly, it doesn't matter list[0] if an object objectX has a reference to another object objectY, objectX will still be eligible for GC if objectX is not referenced by an active reference (its reference to objectY does not matter).

    Ramya Subraamanian wrote:int i refers to a[2], but this will not have effect on GC of Array object a because it is a array of int primitives. And also it doesnt affect the array in the list[1].

    True! But the primitive variable i does not refer to a[2]! Only reference variables can refer to another object. So a copy of the value of a[2] is assigned to primitive variable i.

    Ramya Subraamanian wrote:Now comes the issue with creating the list with asList. There are 2 ways(or more) to create a list using asList.
    asList is created using a new operator like in line 10... which is not like fixed or backed by an array and so even if we remove the element from index 1, it doesnt throw an unsupportedOperationException in line 11. But whereas in line 12 we create a list which is fixed size and backed by array, so when we remove an element it throws the exception in line 13.

    Correct! Although that's not relevant to answer this question correctly.

    Ramya Subraamanian wrote:But now our List is created from asList, which returns a fixed-size list backed by the specified array. changes to the list affects the array. That means there is maybe a reference to that backed array from the list. So even when the list is set to null , there might be reference from that backed array, hence the list and its objects are not garbage collected ....I am not sure about this.

    Using the Arrays.asList convenience method was not a very good choice for this kind of question. Because we don't really know how many objects this method will create. Definitely an instance of a class implementing the List interface, but probably also a temporary array (containing the parameters of the asList method invocation) and maybe even some other objects too. So for simplicity, let's replace this statement with manually creating an ArrayList and adding each element seperately. So after applying these changes, you'll get this code snippet

    Ramya Subraamanian wrote:But if this is true then..only one 1 int Array Object is eligible for GC

    This answer is incorrect. I could provide the correct answer but that would not be very helpful. It's probably better that you carefully read this post and then apply all the above to the code snippet once more and have another shot at the question "how many objects will be eligible for GC after line5 is executed" (of course based on the adjusted/changed code snippet).

    Hope it helps!
    Kind regards,
    Roel
     
    Ramya Subraamanian
    Ranch Hand
    Posts: 178
    17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I got all carried away by the backed array in asList. so lets see.

    int Array object (object 1)
    List Object (object 2)
    list[0] which refers to new String("Java") (object 3)
    list[2] which has new Integer(10) (object 4)
    list[3] which has autoboxed 1000 (object 5)
    StringBuilder object which has new String "Java"(object 6)

    In line 5 , a is null and list is null.

    a is null but list[1] refers to the int Array Object. so Object 1 has a reference so not eligible for GC.
    get(0) is used to return the element at the specified index of 0. So there is no actual reference to list[0] from sb.
    primitive int i holds a copy of value from a[2]. So it has no effect on GC.
    object 2-5 dont have references so they are eligible for GC.
    sb is never set to null so not eligible for GC.

    objects 2, 3, 4, 5. so, 4 objects eligible for GC.




     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ramya Subraamanian wrote:int Array object (object 1)
    List Object (object 2)
    list[0] which refers to new String("Java") (object 3)
    list[2] which has new Integer(10) (object 4)
    list[3] which has autoboxed 1000 (object 5)
    StringBuilder object which has new String "Java"(object 6)

    Everything ok here!

    Ramya Subraamanian wrote:a is null but list[1] refers to the int Array Object. so Object 1 has a reference so not eligible for GC.
    get(0) is used to return the element at the specified index of 0. So there is no actual reference to list[0] from sb.
    primitive int i holds a copy of value from a[2]. So it has no effect on GC.
    object 2-5 dont have references so they are eligible for GC.
    sb is never set to null so not eligible for GC.

    That's all correct as well, except for one statement. But I won't tell which statement is incorrect. That's up to you as it's a very good exercise. I'll provide a tip to discover which statement is wrong further in this post

    Ramya Subraamanian wrote:objects 2, 3, 4, 5. so, 4 objects eligible for GC.

    Because of the incorrect statement, this answer is incorrect as well. Here's a tip to discover which statement was wrong. You have 6 objects in total. An object is eligible for GC if it can't be referenced (accessed) anymore by an active reference. That means if you can write some code after line5 to print the object for example, then the object is not eligible for GC. If you can't write such code, the object is eligible for GC. So let me provide a simple example: after line5 I can add this statement which will print "Java", so object6 (referenced by sb) is not eligible for GC.Now it's up to you to (try to) write similar statements for the other objects. If you can write such a statement, the object is not eligible for GC; otherwise it's eligible for GC.

    Hope it helps!
    Kind regards,
    Roel

    PS. Have a cow for making a drawing for this question. This technique will be very helpful for this kind of questions!
     
    Winston Gutkowski
    Bartender
    Pie
    Posts: 10571
    64
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ramya Subraamanian wrote:I got all carried away by the backed array in asList. so lets see...

    I realise that you probably need this information for the OCJP exam, but I think it's perhaps worth mentioning that this is probably the ONLY time you'll ever need it.

    The whole point of garbage collection is to make things simpler, not more complicated; and the chances are that once you've passed the exam, you'll never need to worry about it again.

    Create objects; use them; let them go out of scope...it really is as simple as that. And if you program in a simple, natural style, 99% of the time it'll be automatic.

    HIH

    Winston
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:
    Ramya Subraamanian wrote:I got all carried away by the backed array in asList. so lets see...

    I realise that you probably need this information for the OCJP exam, but I think it's perhaps worth mentioning that this is probably the ONLY time you'll ever need it.

    Indeed! For the OCAJP (and probably OCPJP as well) you need to know when an object is eligible for GC and at a given point in the code how many objects are eligible for GC. And based on the number of GC-related topics created in this forum, it's probably one of the hardest topics on the exam.
    And although it's so much fun you wonder why it's on the exam. Because as you mentioned: you'll probably never need to worry about GC again.
     
    Ramya Subraamanian
    Ranch Hand
    Posts: 178
    17
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    An object is eligible for GC if it can't be referenced (accessed) anymore by an active reference. That means if you can write some code after line5 to print the object for example, then the object is not eligible for GC. If you can't write such code, the object is eligible for GC.


    I will just go by what you are saying..



    out of the available objects

    object 1 - though it has a reference to the int Array object. it cannot be printed because list is null. and traversing through the list will give a null pointer exception. So it has no active reference(?!?). eligible for GC.

    object 2 - null. so no reference. Eligible for GC

    object 3 - No active reference. cannot be printed. Eligible for GC

    object 4 - No active reference. cannot be printed. Eligible for GC

    object 5 - No active reference. cannot be printed. Eligible for GC

    object 6 - has a active reference. so not eligible for GC. Not Eligible for GC.

    so 5 objects eligible for GC

    I have modified the diagram. Setting the objects inside List to null when List is null might not be right. They just dont have a active reference so eligible for GC. I have a few questions on active reference, but let me first get the answer right
    GCNew_1 (1).jpg
    [Thumbnail for GCNew_1 (1).jpg]
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ramya Subraamanian wrote:I will just go by what you are saying..

    That looks great! One minor remark: when you print a, you don't get a NPE but null is printed (that's probably nothing but a copy/paste issue).

    Ramya Subraamanian wrote:object 1 - though it has a reference to the int Array object. it cannot be printed because list is null. and traversing through the list will give a null pointer exception. So it has no active reference(?!?). eligible for GC.

    Although you are correct that object1 (the int array) is eligible for GC, your explanation is a bit weird. First of all, object1 does not have a reference to the int array (object1 IS the int array). Before line5 you have two (active) references to object1 (the int array): reference variable a and the 2nd element (at index 1) of list. On line5 both reference variables a and list are set to null. So after line5 is executed, you don't have any (active) reference anymore to access object1 (the int array) and therefore object1 is eligible for GC.

    Ramya Subraamanian wrote:object 2 - null. so no reference. Eligible for GC

    object 3 - No active reference. cannot be printed. Eligible for GC

    object 4 - No active reference. cannot be printed. Eligible for GC

    object 5 - No active reference. cannot be printed. Eligible for GC

    object 6 - has a active reference. so not eligible for GC. Not Eligible for GC.

    All spot-on!

    Ramya Subraamanian wrote:so 5 objects eligible for GC

    That's the correct answer!

    Ramya Subraamanian wrote:I have modified the diagram. Setting the objects inside List to null when List is null might not be right.

    The diagram is just to help yourself determine which objects don't have an active reference anymore and thus are eligible for GC. So it doesn't have to be 100% correct. If you understand the diagram, that's the only thing that matters But after setting list to null, its elements won't refer anymore to their objects (and that's clearly visible on your diagram). Have a cow for creating this nice (and correct) drawing!

    Ramya Subraamanian wrote:I have a few questions on active reference, but let me first get the answer right

    Your answer is correct, so just fire all the questions (about active reference) you have

    Hope it helps!
    Kind regards,
    Roel
     
    Ramya Subraamanian
    Ranch Hand
    Posts: 178
    17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    when you print a, you don't get a NPE but null is printed

    yeah, thats right. I missed it.

    without an Active reference, objects get eligible for GC.
    But we are not able to access the value -100 from the previous question right, but still it is not eligible for GC.
    and We can just print null myString value after it is set to null, but it is not eligible for GC.



    Maybe its different when it comes to caching and String Literal Pool.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ramya Subraamanian wrote:without an Active reference, objects get eligible for GC.
    But we are not able to access the value -100 from the previous question right, but still it is not eligible for GC.
    and We can just print null myString value after it is set to null, but it is not eligible for GC.

    You are correct! But the JVM has a String Literal Pool and this pool stores references to all String literals used in your code. When you use such a literal in the code, the reference from the pool is used. So when you set a reference in your code to null (like in your code with the reference variable myString at line2), there is still an active reference to access/reach this String literal (object). But this active reference is kept by the JVM so you as a developer can't access it like I described previously. And exactly the same happens with the primitive wrapper class instances which are autoboxed and cached (e.g. Short and Integer within range of -128 to 127 inclusive). That actually means that String literals and these autoboxed cached primitive wrapper class instances will never be eligible for GC.

    It's hard to provide a crystal-clear code snippet of the above behavior, but I tried my best to come up with this exampleSo based on this example you can clearly see that each occurence of the String literal "myString" has the same reference value (otherwise the == operator would evaluate to false). So even after setting myString to null (on line1), when we reassign the String literal "myString" to myString, it's again the same reference. But if we create a new String instance with the same value as the String literal "myString" (on line2), the reference value of myString2 is different from the String literal "myString". And if we use the intern() method (on line3); the JVM adds this String value to the String Literal Pool. If it's already in the String Literal Pool (like in this code snippet), the reference value of this String literal is returned. And that's why the last line prints true as well.

    Hope it helps!
    Kind regards,
    Roel
     
    Ramya Subraamanian
    Ranch Hand
    Posts: 178
    17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I can understand it much better now. Thank you Roel . good code snippet and explanation ...And Thanks for the cow(s)...
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ramya Subraamanian wrote:I can understand it much better now. Thank you Roel . good code snippet and explanation

    Glad to hear the explanation and code snippet helped you to get a much better understanding. That was the sole purpose of that post
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic