Manoj
Manoj

Reputation: 5867

How to calculate eligibility of objects to garbage collector in Java?

Please clarify my doubt.

class A{
Long weight = 1000L;
}
public class B extends A{
 public static void main (String[] args){
    B b = new B();
    B c = new B();
    b = null; 
    c = null;//going to gc.
 }
}

Here in the above code, while reaching "going to gc" how many objects are eligible for garbage collector? As far as i know args[], b, c and two Long objects totally 5 are eligible. But some says totally 4. They are saying that no two Long object will be created in heap but only one.

Please clarify my doubt.

Upvotes: 3

Views: 821

Answers (6)

Edwin Dalorzo
Edwin Dalorzo

Reputation: 78579

According to the Java Language Specification section 5.1.7 Boxing Conversions:

If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 is the
same as r2.

Therefore it may be true only if this principle applies to long autoboxing of 1000L as well, false otherwise.

This could be the premise of the question, but JLS does not seem to include long autoboxing, neither numbers over 127,

Chances are that you are right an your questioner is wrong as far as I can see.

At any rate, I just wanted to point out that the scenario in question is possible with autoboxing under the conditions stated in the JLS. The conditions on your question do not satisfy the requirements and therefore I'd agree with your elligible object count.

Upvotes: 1

p27
p27

Reputation: 2222

only 4 objects are eligible for gc . at line c = null;//going to gc. set the break point and debug your code in eclipse it will show you how many object will created.

Upvotes: 0

WhiteFang34
WhiteFang34

Reputation: 72039

There are two Long objects. You can verify by adding these lines before you set b and c to null:

System.out.println(System.identityHashCode(b.weight));
System.out.println(System.identityHashCode(c.weight));

You'll get two different identity hash codes meaning they're two different objects.

Upvotes: 0

Dilum Ranatunga
Dilum Ranatunga

Reputation: 13374

This is slightly tricky.

'b', 'c' get garbage collected. +2

Since weight's value is outside of the range [-128, 127], the autoboxing will allocate new Long instances for each 'A' +2 (See http://www.jdocs.com/harmony/5.M5/java/lang/Long.html#823)

If you were invoking B.main using some reflection mechanism, and did not hold references to class B or its Method instances etc, then class A, class B along with all the Method instances etc. +a_bunch

If the invoker does not use the args array passed in, then args; +1

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240870

4 Object[2 of B, 2 of Long] will be created and all 4 would be ready for GC.

At the line

c = null;//going to gc.

there would be live reference of args so args won't be ready

Upvotes: 3

Lyn Headley
Lyn Headley

Reputation: 11588

Well, if b.weight == c.weight then the answer is 4. Otherwise, the answer is 5. Try it and see.

Upvotes: 0

Related Questions