Objects suitable for garbage collection

This question was taken from Kathy Sierra SCJP 1.6 . How many objects are eligible for garbage collection?

According to Katie Sier's answer, this is C This means that two objects have the right to collect garbage. I gave an explanation of the answer. But why is c3 not entitled to garbage collection (GC)?

 class CardBoard { Short story = 200; CardBoard go(CardBoard cb) { cb = null; return cb; } public static void main(String[] args) { CardBoard c1 = new CardBoard(); CardBoard c2 = new CardBoard(); CardBoard c3 = c1.go(c2); c1 = null; // Do stuff } } 

When // Do stuff reached, how many objects are suitable for the GC?

  • A: 0
  • B: 1
  • C: 2
  • D: Compilation error
  • E: Impossible to know
  • F: exception is thrown at runtime

Answer:

  • C is correct. Only one CardBoard object (c1) is available, but it has a Short shell object associated with it, which also has the right to participate.
  • A, B, D, E, and F are incorrect based on the foregoing. (Goal 7.4)
+6
source share
5 answers

The object never existed, as indicated by c3 . The constructor is called only twice, two objects, one to which c1 and c2 point. c3 is just a link that has never been assigned anything but a null pointer.

The c3 link, which currently points to zero, will not go out of scope and be removed from the stack until the final bracket at the end of the main method is completed.

The object originally assigned to c1 is not available because the c1 link was set to null, but the c2 link was not changed, so the object assigned to it is still accessible from this area using c2 .

+6
source

Let me break this line by line:

 CardBoard c1 = new CardBoard(); 

Now we have two objects: CardBoard c1 point and Short c1.story . For GC, there are no c1 points in the CardBoard and story points of CardBoard in Short ...

 CardBoard c2 = new CardBoard(); 

As above, we now have four objects, none of which are available to the GC.

 CardBoard c3 = c1.go(c2); 

We call the method on the CardBoard that c1 points to, passing the value of c2 , which is a reference to the CardBoard object. We reset the parameter, but Java is passed by value, which means that the c2 variable itself is not affected. Then we return a null parameter. c3 null , c1 and c2 are unaffected. We still have 4 objects, none of which can be GC'd.

 c1 = null; 

We have null c1 . The CardBoard object that c1 previously pointed to now no longer points to it, and it could be GC'd. Since the story variable inside this CardBoard object is the only thing pointing to Short , and since this CardBoard object is eligible for GC, Short also becomes suitable for GC. This gives us 4 objects, 2 of which can be GC'd. Objects suitable for the GC are those previously referenced by c1 and c1.story .

+7
source

c3 is null , so there is clearly no object entitled to garbage collection.

Note that only two CardBoard objects are CardBoard , two of these lines:

 CardBoard c1 = new CardBoard(); CardBoard c2 = new CardBoard(); 

and after link juggling, only one of them has no links.

+4
source

Formally, the correct answer is that we do not know. And the reason we don't know is the line:

 Short story = 200; 

The following byte code will compile:

 CardBoard(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: aload_0 5: sipush 200 8: invokestatic #2 // Method java/lang/Short.valueOf:(S)Ljava/lang/Short; 11: putfield #3 // Field story:Ljava/lang/Short; 14: return 

Line 8 is the key here, Short.valueOf() , which returns the boxed equivalent of primitive 200 . Let's take a look at Javadoc Short.valueOf() :

This method will always cache values โ€‹โ€‹in the range of -128 to 127, inclusive, and can cache other values โ€‹โ€‹outside this range.

200 goes out of the "need to cache" range and, therefore, falls under the "can cache". If it is cached, the story value will not be eligible for the GC if it contains an instance of CardBoard . If it is not cached, story will be unavailable and therefore GCed.

To make the question unambiguous (and the proposed answer is correct), the code should be modified as follows:

 Short story = new Short(200); 

Update: 1.6 Javadoc for Short.valueOf() rather more mysterious than the version 1.8 I quoted, but the same logic applies: there is no way to tell, simply by looking at the code, whether a new or cached instance of Short will be returned.

+1
source

If you notice that only two objects are created in the code. c3 is never initialized to the object; this is an empty reference. Therefore, only one โ€œobjectโ€ has the right to garbage collection.

-1
source

Source: https://habr.com/ru/post/920898/


All Articles