• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage Collection question

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this answer your question?
 
Bill Nelsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
What are you saying? I thought you said that Santa gave you that. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic