• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Garbage collection

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when System.gc() is run..is is guaranteed that..
either all the objects eligible for garbage collection will be collected or none will be. Also is there any sequence in which they might be collected?
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing is guaranteed. This is one thing you need to remember.

It is not guaranteed that all objects eligible for garbage collection will be collected, although it is probable that some will when you call System.gc(). The order in which they might be collected is not in any particular sequence.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are very true No�l,

Nothing is guaranteed with Garbage Collection.
With Syste.gc(), you can just make a request to the JVM(correct me if im wrong here), to initiate the Garbage Collection Algorithm which wipes out the unreachable objects from the heap. It may ignore your request altogether!!

ThanQ
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice, just read through this.
http://www.jchq.net/tutorial/03_01Tut.htm

Seems like so far I have noticed that 2 things in Java are not quarantee. Thread and GC (not green card, it's garbage collection)
 
Swati Udas
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually my question was regarding the following code

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


Quest taken from Dan Chisholm's test.
THe correct answer is a,d,e..
I think that if garbage collection does not guarantee that it will collect all the eligible objects then c. should also be a valid answer!!


Also I have one more doubt regarding Garbage Colection:
From the same test..
class I {
private I other;
public void other(I i) {other = i;}
}
class J {
private void m1() {
I i1 = new I(), i2 = new I();
I i3 = new I(), i4 = new I();
i1.other(i3); i2.other(i1);
i3.other(i2); i4.other(i4);
}
public static void main (String[] args) {
new J().m1();
}}

Which object is not eligible for garbage collection after method m1 returns?

a. i1
b. i2
c. i3
d. i4
e. Compile-time error
f. Run-time error
g. None of the above

Here all 4 objects i1,i2,i3,i4 are eligible for garbage collection.. That is ok because they are all method specific variables..but if they were instance variables..it wud just be the case that
i1 has a ref pointing to i3, i2 has a ref pointing to i1 and so on..but they would not be eligible for garbage collection..Am I right??
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think swati is right

In the first case once A1A2A3A1 is printed then any cobination of A1 A2 A3 after that must be a valid answer.

And also in second case:
"i1 has a ref pointing to i3, i2 has a ref pointing to i1 and so on..but they would not be eligible for garbage collection"
is logical...

What say all???
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swati,

Your comments on the first question are correct; option C should be listed as a correct answer. I just uploaded the fix.

Your comments on the second question are also correct. If the objects were referenced by instance variables, then the references would continue to exist after the method runs to completion, and the objects would not be eligible for garbage collection.
 
Swati Udas
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you!!
Makes me feel so confident
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic