• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Garbage Collection

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source enthuware

At which line the object created at line 3 reffered by s will be garbage collected.

1)public class TestClass {
2) public static void main (String args[]){
3) Student s = new Student("Vaishali", "930012");
4) s.grade();
5) System.out.println(s.getName());
6) s = null;
7) s = new Student("Vaishali", "930012");
8) s.grade();
9) System.out.println(s.getName());
10) s = null;
}
}


1)It will not be Garbage Collected till the end of the program.
2)Line 5
3)Line 6 Answer
4)Line 7
5)Line 10



i thought the answer is 4 ie line 7.But they saying the answer is 3 line 6.

At line 6 we are eligible that object by putting that reference to null.
By this time the garbage collector my not run.

But at line 7 s is refering to new object.


Can you give your suggestions on this ?

Thanks

Rao
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rao,

You are right to be confused. The wording of the question is wrong or suggestive at least, it suggests that gc will run and it might not or might or perhaps (it is never guaranteed, even when System.gc() is invoked). What the question should be is when is the object created in line 3 eligible for gc and that is at line 6, since there the only reference to it is set to null. GC doesn't care about whether s will be assigned a new value or not, it only cares about objects not being referenced at all.

Your answer of 7, or rather your explanation is incorrect with basically the same explanation: gc may never run.

Cor

[ June 12, 2007: Message edited by: Cor Takken ]
[ June 12, 2007: Message edited by: Cor Takken ]
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rao,

I dont know how much you know about garbage collection, but just remember that an object is eligible for garbage collection only when it has no reference or it has isolated reference.

In this case, at line 6, there are no more references pointing to the previously created object. So the object becomes eligible for gc at line 6 itself. At line 7, you are simply reassigning the reference variable to a new object. You could have done that even at line 8, 9, ... at whatever point you want. It really doesnt matters.

What matters is the object. If it has got no references, it's of no use hence can garbage collected.

Infact, if the jvm thinks that your app is running out of memory, it will then run gc & get your object garbage collected right at line 6 before executing line 7. But if you app has got lots of memory, then the gc might not run during the entire application & hence the object might never be garbage collected.
 
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
Hi Rao,
As Cor said its perfectly a point of confusion being made in the question. Only what we can say is "the objects being eligible for garbage collection"!

At line 6, you are setting the reference variable s to null. In this case, the object being pointed by s till now is left unreferenced by any other references. So its eligible for garbage collection.

In the lines below, you are creating a brand new object and assigning the object to the same reference variable s. But still it does not matter or relate to the object which s was previously pointing to!

HtH.
[ June 12, 2007: Message edited by: Raghavan Muthu ]
 
Author
Posts: 3473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also If you have a circular reference of objects, but you no longer reference it from an execution thread, then this object will be a potential candidate for garbage collection.
 
This will take every ounce of my mental strength! All for a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic