Reputation: 2911
The following code creates one array and one string object.
Now my questions are
Here is my code
String[] students = new String[10];
String studentName = "Peter Smith";
students[0] = studentName;
studentName = null;
I was thinking the answer is only one object i.e. students
But according to Oracle docs, Neither object is eligible for garbage collection
How should I infer the answer?
Upvotes: 4
Views: 274
Reputation: 29213
How many references to those objects exist after the code executes?
String[]
, obtainable through the expression students
.String
, obtainable through the expression students[0]
.Why?
Essentially, the answer is that objects, not variables, can ever be eligible for garbage collection.
In your case, you have copied a reference of the string (which is an object) into the first slot of the array. Even after you clear (set to null
) the initial variable, that same object is still visible from your code, by another name (and by "name" here I mean "expression", as described above). This is why the string is still not eligible for garbage collection.
For comparison, consider your code without the third line:
String[] students = new String[10];
String studentName = "Peter Smith";
studentName = null;
In this scenario, the string "Peter Smith"
would indeed be eligible for garbage collection as you'd expect, because it's not obtainable anymore by any expression.
(All the above concern the Java language and leave any possible JVM optimizations aside.)
Upvotes: 10
Reputation: 37364
String[] students = new String[10];
// 1. One object (array of String, 1 reference to it - students)
String studentName = "Peter Smith";
// 2. Two objects (array from (1), string 'Petter Smith'), two references
// (students from (1), studentName that refers String object )
students[0] = studentName;
// 3. Two objects (the same as (2)), 3 references ( in addition to references (2)
// first element of students array refers to the same object as studentName
studentName = null;
// Two objects (Array and String "Peter Smith"), two references (one is array,
// another is students[0]
// Neither of them can be marked for garbage collection at this point
// (Array and String "Peter Smith" )
I hope that makes sense.
Upvotes: 4
Reputation: 7940
An object can have multiple references, even though you set reference of studentName
to null the String object is still being referenced by student[0]
. So the String "Peter Smith"
can't be garbage collected.
Upvotes: 3
Reputation: 2260
students
is instantiated and then altered, so there is no reason for it to be collected because we still have a reference. studentName
is instantiated, then the reference is assigned to students
, so when it is dereferenced in the last line, a reference to the object, the string, still exists in the array, that's why nothing is collected by the GC.
Upvotes: 1
Reputation: 1188
You are right that students
is an object, but it an object of type String array. In an array each element of an array can reference other objects, and the first element `students[0]' is referencing the string object containing "Peter Smith" so that object is still being referenced and thus not eligible for garbage collection. When students goes out of scope and becomes eligible for GC itself then the string object will too.
Upvotes: 1
Reputation: 4808
Here "Peter Smith
is an object and assigned to students[0]
so it cannot be garbage collected and studentName=null
, it points to nothing so no trying garbage collect it.
So, both cannot be garbage collected.
Upvotes: 3