About Question enthuware.ocpjp.v21.2.4012 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
skuranov1
Posts: 1
Joined: Mon Feb 03, 2025 9:37 am
Contact:

About Question enthuware.ocpjp.v21.2.4012 :

Post by skuranov1 »

I cannot understand why the correct answer is 0. I understand that n1 and n2 cannot be garbage collected as n2 is used now and it holds a pointer to n1. But n3 should be collectable because there is no place in the program where it is used after initialization. So, the correct answer should be 1?

admin
Site Admin
Posts: 10383
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v21.2.4012 :

Post by admin »

The instance pointed to by n3 will indeed be GCed but only after the main method ends. The problem statement is talking about the time when the main method is still executing System.out.println(n2.dump()); n3 is not eligible to be GCed at this point.

dameest
Posts: 16
Joined: Sat Nov 30, 2024 5:27 am
Contact:

Re: About Question enthuware.ocpjp.v21.2.4012 :

Post by dameest »

admin wrote:
Tue Feb 04, 2025 1:09 am
The instance pointed to by n3 will indeed be GCed but only after the main method ends. The problem statement is talking about the time when the main method is still executing System.out.println(n2.dump()); n3 is not eligible to be GCed at this point.
Hello, you said that n3 will be GCed after the main method ends, but I don't think so. Is there any reference doc that supports this behaviour? I dont think local object are collected at the end of method execution.

From java spec, objects a GCed when they are no longer reachable (which is the case for n3 which is not referenced after being created). The question states "while the program prints 'from dump'" which means "n2.dump()" has already returned from the call and we are now executing "System.out.println()" method.

imagine if we replace for example "System.out.pritln(n2.dump())" by "longRuningMemoryConsuming(n2.dump())". In your example, n1, n2 and n3 are no longer reachable when entering "longRuningMemoryConsuming()" call and are therefore eligible for GC. What do you think?

admin
Site Admin
Posts: 10383
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v21.2.4012 :

Post by admin »

What you are saying is technically correct. Section 12.6.1 of JLS 21 says,
"Optimizing transformations of a program can be designed that reduce the number of objects that are reachable to be less than those which would naively be considered reachable. For example, a Java compiler or code generator may choose to set a variable or parameter that will no longer be used to null to cause the storage for such an object to be potentially reclaimable sooner."

So yes, an optimizing compiler may set these references to null.

However, for the purpose of OCP certification exam, you need to ignore compiler optimizations and go with the assumption that unless a local variable is set to null (or made to point to another object), the object pointed to by that variable is not eligible for GC.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 9 guests