This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "garbage collection" Watch "garbage collection" New topic
Author

garbage collection

Suhita Reddy
Ranch Hand

Joined: Jun 09, 2006
Posts: 60
Hi all,

after line 6 runs,how many objects r eligible for garbage collection?
1.public class X{
2.public static void main(String args[]){
3.X x=new X();
4.X x2=m1(x);
5.X x4=new X();
6.x2=x4;
7.doComplexStuff();
8.}
9.static X m1(X mx){
10.mx=new X();
11.return mx;
12.}
13.}
Karol Kisielewski
Greenhorn

Joined: Jun 28, 2006
Posts: 8
Originally posted by Suhita Reddy:
Hi all,

after line 6 runs,how many objects r eligible for garbage collection?
1.public class X{
2.public static void main(String args[]){
3.X x=new X();
4.X x2=m1(x);
5.X x4=new X();
6.x2=x4;
7.doComplexStuff();
8.}
9.static X m1(X mx){
10.mx=new X();
11.return mx;
12.}
13.}


Only one and it is object created by m1 method (line 4)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Agreed. If you assume that the System.gc() calls work as expected in the following code - you can see it happen:



Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Regarding the above test: run it at the command line rather than using an IDE.
 
GeeCON Prague 2014
 
subject: garbage collection