aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection question" Watch "Garbage Collection question" New topic
Author

Garbage Collection question

Bill Nelsen
Greenhorn

Joined: Aug 11, 2004
Posts: 27
This is a question from Dan Chisholm's practice exams.

class A {
private String name;
private A otherA;
public A(String name) {this.name = name;}
public void other(A otherA) {this.otherA = otherA;}
public A other() {return otherA;}
public String toString() {return name;}
protected void finalize() {System.out.print(name);}
}
class B {
public static void m1() {
A a1 = new A("A1"), a2 = new A("A2"), a3 = new A("A3"), a0 = a3;
a1.other(a2); a2.other(a3); a3.other(a1);
for(int i = 0; i<4; i++){System.out.print(a0 = a0.other());}
}
public static void main(String[] args) {m1(); System.gc();}
}

Which of the following could be a result of attempting to compile and run the program?
a. A1A2A3A1
b. A0A0A0A0A1A2A3
c. A1A2A3A1A2A3
d. A1A2A3A1A1A2A3
e. A1A2A3A1A3A2A1
f. A0A1A2A3A1A2A3

The answer differs from his website and his book. His book states to select 3, while the website allows for 4 answers. The website answer is ACDE and the book's answer is ADE

The printed answer is the same for both the book and website:

The three instances of class A form an isolated ring where each instance references the next instance and the third references the first instance. Four iterations of the for loop are processed. Inside the body of the for statement, the invocation of the print method contains the argument expression a0 = a0.other(). On the first iteration, the reference variable a0 references the instance named A3. The value returned by the method named other is a reference to the instance named A1. The reference is assigned to the reference variable a0 and is also the value produced by the expression a0 = a0.other(). That reference value is passed as an argument to the print method, and the print method invokes the A.toString method. With each iteration of the loop, the reference moves to the next object in the loop and the name of the object is printed. After four iterations, the loop ends and the method m1 returns. The invocation of the System.gc method serves as a suggestion that the garbage collector should be allowed to run. The system could ignore the suggestion, so there is no guarantee that the eligible arguments will be garbage collected. If they are collected, there is no guarantee which will be collected first. The ONLY GUARANTEE is that the finalize method will be invoked on each particular instance before the resources that had been allocated to that instance are reclaimed.

The last line appears to state that the answer "C" can't be correct and that the answer on the website is wrong. That either ALL instances are finalized at once or nothing is. Do I understand this correctly??
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Does this answer your question?


SCJP Tipline, etc.
Bill Nelsen
Greenhorn

Joined: Aug 11, 2004
Posts: 27
I read through all of the postings that you pointed to and it appears that Dan's exercise is pointless. There is NO GUARANTEE about anything with garbage collection.

Makes you wonder why Sun even bothered to create a finalize() or the System.gc() methods. If you can't count on something to work in a reliable manner, than IMHO it is simply better that it doesn't exist at all.

Thanks for the pointer to the other posting.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by John MacNeille:
I read through all of the postings that you pointed to and it appears that Dan's exercise is pointless. There is NO GUARANTEE about anything with garbage collection.

Makes you wonder why Sun even bothered to create a finalize() or the System.gc() methods. If you can't count on something to work in a reliable manner, than IMHO it is simply better that it doesn't exist at all.

Thanks for the pointer to the other posting.


Well, you have some guarantees when it comes to garbage collection. You are guaranteed that, before any object is collected, it's finalize method will be invoked. You are not, however, guaranteed that an object will be collected. Ever.

The fact that Java offers a garbage collection "service" is a boon to the programmers in that you do not need to concern yourself with allocating and deallocating memory (as you would if you were programming in C++). With that service come some rules, though.

Some things do, in fact, work very reliably, but there are a lot of things that simply are not guaranteed, when it comes to garbage collection. Keep that in mind.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
If you actually try this code, it will finalize all three objects every time. This is probably just a service to people studying Java and to avoid endless questions to the Sun Java group.

The real purpose of System.gc() is not memory reclamation but to allow programs to get any garbage collection done just before entering a highly time-sensitive segment. If there is no likelyhood of garbage collection occurring soon, there is no reason for System.gc() to do anything.

If you are actually running short of memory, the jvm will always run the garbage collector before throwing an exception and terminating your program. Given the enormous size of the virtual memory space, this is not very common.


Mike Gershman
SCJP 1.4, SCWCD in process
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection question