aspose file tools*
The moose likes Java in General and the fly likes Basics on how Garbage collector works. 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 » Java » Java in General
Bookmark "Basics on how Garbage collector works." Watch "Basics on how Garbage collector works." New topic
Author

Basics on how Garbage collector works.

Tabassum Momin
Greenhorn

Joined: Oct 10, 2012
Posts: 5
Hello everyone,
I came across this question on net.

class C
{
public static void main(String a[])
{
C c1=new C();
C c2=m1(c1);
C c3=new C();
c2=c3; //6
anothermethod();
}
static C m1(C ob1){
ob1 =new C();
return ob1;
}
}
After line 6, how many objects are eligible for garbage collection?

--> I feel that answer of this question must be 1. I want to know whether I am correct or not.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41579
    
  54
Not a direct answer, but maybe this helps: http://www.coderanch.com/t/266329/java-programmer-SCJP/certification/garbage-Collector. It's certainly fun :-)


Ping & DNS - my free Android networking tools app
Tabassum Momin
Greenhorn

Joined: Oct 10, 2012
Posts: 5
Thank You for sharing that link. It was really helpful and interesting too...
As far, what I understood from that discussion is :
-->The object created within m1() method is the only object ready for garbage collection after the execution of line 6.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7677
    
  19

Tabassum Momin wrote:As far, what I understood from that discussion is :
-->The object created within m1() method is the only object ready for garbage collection after the execution of line 6.

Sounds right to me.

Basically, an object is available for garbage collection as soon as there is no reference in scope that points to it. And in order for a reference to be "in scope" (and I'm not sure if that's the correct term, but it's the way I always think of it) it has to either:
(a) have a name that can be used.
(b) be returned from a method as part of a compound statement, in which case it will be in scope for as long as it's needed in the statement.

For example:
BigInteger cube256 = BigInteger.valueOf(256L).pow(3);
returns an unnamed BigInteger object from valueOf(), which is then used to return its cube (a different object). The unnamed object will be eligible for gc as soon as the pow() method has finished, but the one assigned to cube256 will only be eligible when either:
  • cube256 is reassigned, or
  • no piece of code can "see" code256 any more.

  • Quite honestly, unless you need this information for passing the SCJP exam, it's rarely something you need to obsess about. If you follow good programming practises and avoid using lots of static fields, the chances of you holding on to objects for a lot longer than you need to is fairly slim.
    One exception is Closeable objects (see java.io.Closeable), which you should remember to close() as soon as you're finished with them. And if you're using version 7, there is the new try-with-resources statement that helps you out with that stuff.

    HIH

    Winston


    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
    Tabassum Momin
    Greenhorn

    Joined: Oct 10, 2012
    Posts: 5
    Thank You. I am right now, preparing for SCJP exam, your suggestion will help me to focus on right path.
    It is very well explained and had cleared all my doubts
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7677
        
      19

    Tabassum Momin wrote:Thank You. I am right now, preparing for SCJP exam, your suggestion will help me to focus on right path.
    It is very well explained and had cleared all my doubts

    Glad to help.

    Winston
     
    Don't get me started about those stupid light bulbs.
     
    subject: Basics on how Garbage collector works.