Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Question about garbage collection

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Question about garbage collection

    Dear programmers

    While navigating in the internet I have seen the following Question:

    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?

    Answer: 2

    Maybe someone can tell me why the answer is 2?
    I seems to me that the answer should be 1
    (only c2 is eligible)


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Saint-Petersburg, Russia
    Posts
    33
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Question about garbage collection

    I agree with you. It looks like here is error in problem statement or proposed answer. The same problem is several times mentioned on the web and it looks with the same considerations...

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question about garbage collection

    Thanks a lot for the answer

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Question about garbage collection

    No, I think the answer is correct.
    We start off by initializing c1 to some C object (we'll call this: original_c1).
    We then initialize c2 by calling the m1() method. The m1 method takes c1 as the parameter and changes its reference to some new C object, then returns the new c1 value, which sets c2 to be equal to c1's new value (original_c2). Since c2 and c1 are now equal, there are no more references left to original_c1, so that can be garbage collected.
    Then we initialize c3 to some C object.
    Lastly, we set c2 to be equal to c3. So no more references to original_c2 exist, and that can also be garbage collected.

    So I agree with 2, and the two objects eligible for garbage collection are c1 and c2's original initialization values.

    EDIT: or maybe not if c1 is still referencing original_c2...
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Question about garbage collection

    Prove it to yourself - there is a method in the API which is "Called by the garbage collector on an object when garbage collection determines that there are no more references to the object." One could use this method, along with a gc call to support any claims as to how many objects of class C are eligible

    class C{
    	public static void main(String a[]) throws Exception{
    		C c1=new C();
    		C c2=m1(c1);
    		C c3=new C();
    		c2=c3; //6
    		System.gc();
    		Thread.sleep(1000);
    		anothermethod();
    	}
     
    	static C m1(C ob1){
    		ob1 =new C();
    		return ob1;
    	}
     
    	@Override
    	public void finalize(){
    		System.out.println(toString() + " Finalized");
    	}
    	static void anothermethod(){
     
    	}
    }

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Question about garbage collection

    I ran the code (with minor modifications). The object created in the m1 method is the only one that gets garbage collected. I was incorrect when I said the c1 variable's reference would change (that's really obvious to me to be wrong now, I don't know why I thought it would).
    If you take out line 6, nothing gets garbage collected.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question about garbage collection

    Aussiemcgr and Copeg thanks for helping me.
    I have also tested the code with finalize and saw that the garbage collection removed only one object.

    I have learnt something new.

    Thanks a lot

Similar Threads

  1. Replies: 1
    Last Post: June 12th, 2013, 12:58 PM
  2. Question : Collection
    By shaw.89ram in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 18th, 2012, 12:24 PM
  3. Does garbage collection occurs in PERM Area of Java Heap ?
    By javabuddy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 22nd, 2011, 12:27 AM
  4. Java Garbage Collection and destructors
    By riddhik84 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 1st, 2009, 09:06 AM
  5. Garbage Collection?
    By kalees in forum Collections and Generics
    Replies: 6
    Last Post: September 23rd, 2009, 03:07 AM