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?
the String literal "myString" is (as you should know) still referenced from the String Literal Pool and therefore is not eligible for GC.
Which objects can be created without using the new keyword
Ramya Subraamanian wrote:So if myString was something like this, then it would have been eligible for garbage collection.
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.
Ramya Subraamanian wrote:I think the answer is the objects created using autoboxing.
Ramya Subraamanian wrote:I will remember all the rules.
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.
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:
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.
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:So we can expect no questions on GC with String !!!
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.
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).
salvin francis wrote:I would love to be a part of that quiz ...
salvin francis wrote:
Strings : String str = "hello"; Autoboxing: Integer number = 12;
salvin francis wrote:Am I missing something in my tiny list ?
Yes, null is not an instance of Object
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).
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.
Ramya Subraamanian wrote:sorry about the unncessary confusion.
Ramya Subraamanian wrote:So ..I correct my answer..2 objects eligible for GC
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
Have another close look at the code snippet, maybe you see a bunch of other objects which might be eligible for GC
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.
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.
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.
Ramya Subraamanian wrote:2 Array Objects+ 1 Object from Short array+ 3 Objects from Double Array = 6 Objects eligible for GC.
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)
Ramya Subraamanian wrote:list[0] - new String("Java") - A String created with new, hence eligible for GC(object 3)
Ramya Subraamanian wrote:list[1] - Array object a, eligible for GC.(object 4)
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)
Ramya Subraamanian wrote:String Builder object -sb which is created which points to list[0] (object 7)
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.
Ramya Subraamanian wrote:list[0] - has a reference to StringBuilder sb so will not be GCed.
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].
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.
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.
Ramya Subraamanian wrote:But if this is true then..only one 1 int Array Object is eligible for GC
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)
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.
Ramya Subraamanian wrote:objects 2, 3, 4, 5. so, 4 objects eligible for GC.
Ramya Subraamanian wrote:I got all carried away by the backed array in asList. so lets see...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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.
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.
Ramya Subraamanian wrote:I will just go by what you are saying..
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.
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.
Ramya Subraamanian wrote:so 5 objects eligible for GC
Ramya Subraamanian wrote:I have modified the diagram. Setting the objects inside List to null when List is null might not be right.
Ramya Subraamanian wrote:I have a few questions on active reference, but let me first get the answer right
when you print a, you don't get a NPE but null is printed
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.
Ramya Subraamanian wrote:I can understand it much better now. Thank you Roel . good code snippet and explanation