About Question enthuware.ocpjp.v21.2.4012 :
Moderator: admin
-
- Posts: 1
- Joined: Mon Feb 03, 2025 9:37 am
- Contact:
About Question enthuware.ocpjp.v21.2.4012 :
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?
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v21.2.4012 :
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.
-
- Posts: 16
- Joined: Sat Nov 30, 2024 5:27 am
- Contact:
Re: About Question enthuware.ocpjp.v21.2.4012 :
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?
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v21.2.4012 :
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.
"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.
Who is online
Users browsing this forum: Google [Bot] and 12 guests