Results 1 to 2 of 2
  1. #1
    SamJava_the_Hut is offline Senior Member
    Join Date
    Jul 2013
    Wisconsin, USA
    Rep Power

    Default Understanding the rules of garbage collection

    This source explains the criteria for objects that are eligible for garbage collection:

    Unfortunately, the resulting output of my code doesn't entirely match my expectations:
    Java Code:
    package samsExperiments;
    interface Animal{
    	void makeNoise();
    class Horse implements Animal{
    	int weight = 300;
    	public void makeNoise() {
    public class NonStaticGarbageCollectorTest extends Horse {
    	String name;
        public NonStaticGarbageCollectorTest(String name)  
   = name;
    	public static void main(String[] args) {
    		NonStaticGarbageCollectorTest x1 = new NonStaticGarbageCollectorTest("WhiteHorse");
    		NonStaticGarbageCollectorTest x2 = new NonStaticGarbageCollectorTest("BlackHorse");
    		NonStaticGarbageCollectorTest x3 = new NonStaticGarbageCollectorTest("BrownHorse");
    		NonStaticGarbageCollectorTest x4 = new NonStaticGarbageCollectorTest("GreenHorse");
    		//geekdsforgeeks 2, Reassigning the reference variable:
    		x3 = x1;//x3 should now be "WhiteHorse"; original x3 [BrownHorse] now eligible for garbage collection?
    		System.out.println("x3 = " +;
    		x1 = x2;//x1 should now be "BlackHorse"; original x1 [WhiteHorse] now eligible for garbage collection?
    		System.out.println("x1 = " +;
    		//geeksforgeeks 3, Nullifying the reference variable
    		if("BlackHorse")) {
    			System.out.println("x2 still has it's original value of " + + " stored in it.");
    		x2 = null;//unlike x3 and x1, the value in x2 was never reassigned to another object's value,
    				  //but shouldn't setting it to null make it eligible for garbage collection?
    		x4 = null;
    		//geeksforgeeks 4, Anonymous object:
    		new NonStaticGarbageCollectorTest("BlueHorse"); 		
    		// calling garbage collector 
        /* Overriding finalize method to check which object 
        is garbage collected */
        protected void finalize() throws Throwable  
            //print the attributes of each GarbageCollectorTest object: 
            System.out.println( + " successfully garbage collected");
    x3 = WhiteHorse
    x1 = BlackHorse
    x2 still has it's original value of BlackHorse stored in it.
    GreenHorse successfully garbage collected
    BlueHorse successfully garbage collected
    BrownHorse successfully garbage collected

    x2 (BlackHorse) was never overwritten or reassigned, but it was assigned to null on line 40, so why was it not garbage collected like x4 was on line 43?

    Also, x3 (WhiteHorse) on line 30 and x1 (WhiteHorse) on line 32 both had their reference variables reassigned there, so why wasn't x1 also successfully garbage collected like x3 was according to the output?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: Understanding the rules of garbage collection

    GC is one of the last things to worry about. I've never had any issues with GC.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. garbage collection
    By shantharam in forum New To Java
    Replies: 4
    Last Post: 06-28-2013, 07:32 PM
  2. Weird Garbage Collection...
    By fatabass in forum New To Java
    Replies: 16
    Last Post: 12-20-2011, 09:59 PM
  3. JVM Garbage Collection
    By daromnet in forum Advanced Java
    Replies: 4
    Last Post: 03-09-2011, 10:18 AM
  4. Garbage Collection Question
    By DrMath in forum New To Java
    Replies: 5
    Last Post: 10-03-2009, 10:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts