This week's book giveaway is in the Web Services forum.
We're giving away four copies of Microservices in Action and have Morgan Bruce & Paulo A. Pereira on-line!
See this thread for details.
Win a copy of Microservices in Action this week in the Web Services forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Garbage Collection Question  RSS feed

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is a question from Whizlabs simulator:

In the following code, which is the earliest statement, where the object originally held in m, may be garbage collected?

public class Test {
public static void main(String[] args) {
MyObject m = new MyObject("Sun", 48);
m.myMethod();
System.out.println("Working");
m = null;
m = new MyObject("Moon, 36); //Line 1
m.yourMethod();
System.out.println("Testing");
} //Line 2
}



The answer given by Whizlabs is Line 1. But is Line 2 a better answer as you cannot say what happens by calling m.myMehtod(). Suppose this method passes the reference of the current object, say someOtherMehtod(this); , what effect would making m=null in terms of garbage collection? Can you say the object is eligible for garbage collection as the reference passed by someOtherMethod(this); is possibly active? Please clarify. Thanks!
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have very good point.


class SomeOtherObject {
private static List<MyObject> lst = new ArrayList<MyObject>();

public static void someOtherMethod(MyObject o) {
lst.add(o);
}
}

class MyObject {
void myMethod() {
SomeOtherObject.someOtherMethod(this);
}
...
}

public class Test {
public static void main(String[] args) {
MyObject m = new MyObject("Sun", 48);
m.myMethod();
System.out.println("Working");
m = null;
m = new MyObject("Moon, 36); //Line 1
m.yourMethod();
System.out.println("Testing");
} //Line 2
}


I will say after Line2 in this case.
[ May 14, 2006: Message edited by: wise owen ]
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I had the same doubt when I did the whizlabs practice test but I think there is one very important point we must note here:

public class Test {
public static void main(String[] args) {
MyObject m = new MyObject("Sun", 48);
m.myMethod();
System.out.println("Working");
m = null;
m = new MyObject("Moon, 36); //Line 1
m.yourMethod();
System.out.println("Testing");
} //Line 2
}

any method that is called from main method is guaranteed to complete before main is completed. Therefore any references created in those methods will be eligible for GC much more earlier than the ref.s in main except if pass that ref. back to the calling method and store it in some variable. In this question we are not getting any ref.s back from the method "myMethod", therefore, we should be sure of the thing that even if any ref. of "this" was passed to some other method it would have been garbage collected till now and there is no harm in GCing this object after line 1 as soon as the ref. is set to null.

Hope this helps
Asha
 
Jacob Thomas
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

...
we should be sure of the thing that even if any ref. of "this" was passed to some other method it would have been garbage collected till now and there is no harm in GCing this object after line 1 as soon as the ref. is set to null.



I am sorry but I am very much confused now. Two replies with contradictory statements. Could anybody please give a final verdict?
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Asha:

In the following code, there is not object created. I just simple pass "this" to a static function and which will save the pass in object's reference to a static array.

How can GC collect it before Line 2?


class SomeOtherObject {
private static List<MyObject> lst = new ArrayList<MyObject>();

public static void someOtherMethod(MyObject o) {
lst.add(o);
}
}

class MyObject {
void myMethod() {
SomeOtherObject.someOtherMethod(this);
}
...
}
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!