Thursday, December 7, 2006

Java Garbage Collection: How it works, How to control it


Java has Garbage Collection. Good thing, yes! But, say with me:


The Garbage Collector deallocates all objects with zero references when it wants!!

Two troubles:

  • Objects with zero references: typically a Java code looks like:
    public void doSomething() {
    Configuration c = new Configuration("test.conf");
    long limit = c.getLimit();

    for(long i = 0; i≤limit ; i++) {
    String s = new String(i);
    System.out.println(s);
    }
    }

    The Configuration instance lives until the method's end. We can write:
    public void doSomething() {
    Configuration c = new Configuration("test.conf");
    long limit = c.getLimit();
    c = null; // good! now c has 0 reference!

    for(long i = 0; i≤limit ; i++) {
    String s = new String(i);
    System.out.println(s);
    }
    }

  • GC deallocates them when it wants: it seems that the String instances will be cleaned when they go out of scope, but it isn't!!.
    The String instances are in a particular state called Invisible State.
    The GC cleans all the object that go out of scope only when the method doSomething() returns!!
    Simply, manually set the reference to null:
    public void doSomething() {
    Configuration c = new Configuration("test.conf");
    long limit = c.getLimit();
    c = null; // good! now c has 0 reference!

    for(long i = 0; i≤limit ; i++) {
    String s = new String(i);
    System.out.println(s);
    s = null; // good! now s has 0 reference!
    }
    }

    Now all our Object are only eligible for cleaning.
    You cannot force garbage collection, just suggest it! You can suggest GC with System.gc(), but this does not guarantee when it will happen.
Interesting links: Sun's Truth About Garbage Collection,

5 comments:

gdss said...

I don't see any problem. This is a design choice. Why does gc have to clean up an object when you set its handler to null?

Ricibald said...

it's true! In general this is not a problem. It's important only when you use an object that uses a lot of RAM. In this case it's better to take care of these details.

Anonymous said...

Hey Riccardo!!

Your Blog rocks!! I have a query…
- “Seeking experienced programmer with following skills: strong browser UI background, specifically in JavaScript, AJAX, CSS, HTML. Also must have good system & software design skills, e.g. OOP. C++/C knowledge a plus”

There is this amazing site that I came across where u can make money by answering this query and sharing information…check it out here’s the link http://www.myndnet.com/login.jsp?referral=alpa83&channel=AA29

The coolest part is…every time ur information gets sold u get paid for it!!
I signed it for it.. very cool stuff… u can also mail me at barot.alpa@gmail.com

Cheers!
Alpa

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

Anonymous said...

@the above two guys -
Please post comments which are readable to all. This blog has international readership.