• Post Reply Bookmark Topic Watch Topic
  • New Topic

garbage collection

 
Suhita Reddy
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed. If you assume that the System.gc() calls work as expected in the following code - you can see it happen:

 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding the above test: run it at the command line rather than using an IDE.
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!