• 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

about gc()

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai,
public class Test{
public static void main(String[] args) {
Object a = new Object();
Object b = new Object();
Object c = new Object();
Object d = new Object();
d=c=b=a;
d=null;
}
}

How many objects are eligible for GC after d = null?
can anybody tell this
Thanks

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
three.
Four objects are created.
When the assgn. takes place
d=c=b=a;
After this three Objects are eligible.
When d= null is executed,
the reference to the OBject A is reduced by 1. since b,c and a still have refernces, Object A is not eligible for gc(). At the end of the block, all the four are eligible, but not until then.
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't even need d=null to get 3. With the assignment, d=c=b=a, you have the variable "a" reference pointing to it's original object. One you assigned the other variable reference to the same object as the "a" variable, then they lost reference to their object which makes those objects available for GC.

Output is:
finalize Foo(2)
finalize Foo(3)
finalize Foo(4)
-Peter
 
Lakshmi Ram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai,
Thank U peter,sridevi.
But ,can u explain it more .I didn't understand it clearly
thanks
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
when the statement d=c=b=a executes,The d,c,b are referenced to 'a' like d->c->b->a,finally all referenced to 'a'.so they lost their old actual positions,because they are nomore referenced to the actual values.so they are garbage collected.That's why peter saying that you don't even need the d is null statement.
I hope this helps.
Sri
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
In case, u need more explanation, then u can read the following. I have tried to explain it in a simple way.
Object a = new Object();
Object b = new Object();
Object c = new Object();
Object d = new Object();
Imagine that the Object reference variable "a" refers to an Object Ob1.
Similarly, "b" refers to an Object Ob2. "c" refers to an Object
Ob3. "d" refers to an Object Ob4.
After the statement, d=c=b=a
"a" still refers to Ob1.
"b" also refers to Ob1.
"c" also refers to Ob1.
"d" also refers to Ob1.
So the objects Ob2, Ob3 and Ob4 are left unreachable(not used). That makes the 3 (Ob2,Ob3,Ob4) Objects eligible for garbage collection.
Hope this will do.
All the best
regards
nidiya
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nidiya,
I can't see how I could make it any clearer than you have. Good job!
-Peter
 
Lakshmi Ram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai sri,nidiya
Thank u all .u did a good job.Now i am clear.
Thanks
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mport java.io.*;
class Foo {
int i;
Foo(int i) {
this.i = i;
}
protected void finalize() {
System.out.println("finalize Foo("+i+")");
}
}
public class GC{
public static void main(String[] args) {
Foo a = new Foo(1);
Foo b = new Foo(2);
Foo c = new Foo(3);
Foo d = new Foo(4);
d=c=b=a;
// after assignment b, c, and d don't point to their own objects anymore.
// d = null; // not needed!!!
// Try to get GC to run by calling it a bunch of times.
System.gc();
System.gc();
System.gc();
}
}
Output is:
finalize Foo(2)
finalize Foo(3)
finalize Foo(4)

Should the output not be
finalize Foo(1)
finalize Foo(1)
finalize Foo(1)
Be kind this is my first post!!!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic