• Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage Collection on object with null reference.  RSS feed

 
M Zia
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question which relates to the following exercise from Java tutorial

{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{

1.

The following code creates one array and one string object. How many references to those objects exist after the code executes? Is either object eligible for garbage collection?

...
String[] students = new String[10];
String studentName = "Peter Parker";
students[0] = studentName;
studentName = null;
...

}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}

The string object above called studentname should be garbage collected now since it
points to null. however the answer book on the above says neither objects will be garbage
collected. do you know what is really happening???
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The 2 Objects created in the code snippet are:

(1) String array capable of holding 10 String objects
(2) String literal "Peter Parker"

As students refers to (1) and students[0] refers to (2), neither is available for garbage collection.
 
M Zia
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.
 
M Zia
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James,
The "String studentname" is not an object in this case and will not be garbage collected
after setting it to null??
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

studentName is itself not an object, it is a reference to one. Setting it to null simply means it does not refer to any object. I think the important concept to grasp is that objects are garbage collected, not references to objects.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Zia wrote:James,
The "String studentname" is not an object in this case and will not be garbage collected
after setting it to null??

studentname is not an object - it is a reference variable. Reference variables are never garbage collected - only objects can be garbage collected and this happens when there are no longer any references to them.
Setting a reference variable to null means that it no longer references an object and that object may be eligible for garbage collection if there are no other references to it.
 
M Zia
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James, Joanne,
Thanks for replying so fast. I really appreciate it.
So in the above code, "students" refers to an array of 10 String objects.
When and if we set the "students" reference to null by setting students = null,
then the array of 10 String objects will be garbage collected?? or do we
have to set every member of the array to null in a for loop??
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the array reference is set to null, and there are no other references to any of its elements, then those 11 objects are eligible for garbage collection. If it is a small application, it will not be collected as garbage because the JVM will terminate before there is any need for garbage collection.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Zia wrote:So in the above code, "students" refers to an array of 10 String objects.

To be precise it is an array of 10 String references, only one of which currently refers to a String object.

Campbell Ritchie wrote:If the array reference is set to null, and there are no other references to any of its elements, then those 11 objects are eligible for garbage collection.

Assuming the array elements refer to objects. In this case only one of them does and as that object is a String literal it wont ever be garbage collected. So only the array object itself would be eligible for garbage collection.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I missed the bit about String literals. Sorry.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!