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

    Default Understanding the rules of garbage collection

    This source explains the criteria for objects that are eligible for garbage collection:
    https://www.geeksforgeeks.org/how-to...ge-collection/

    Unfortunately, the resulting output of my code doesn't entirely match my expectations:

    NonStaticGarbageCollectorTest.java
    Java Code:
    package samsExperiments;
    interface Animal{
    	void makeNoise();
    }
    
    class Horse implements Animal{
    	int weight = 300;
    	public void makeNoise() {
    		System.out.println("AAAAAAAH");
    	}
    }
    
    public class NonStaticGarbageCollectorTest extends Horse {
    	
    	String name;
          
        public NonStaticGarbageCollectorTest(String name)  
        { 
            this.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 = " + x3.name);
    		x1 = x2;//x1 should now be "BlackHorse"; original x1 [WhiteHorse] now eligible for garbage collection?
    		System.out.println("x1 = " + x1.name);
    		
    		//geeksforgeeks 3, Nullifying the reference variable
    		
    		if(x2.name.toString().equals("BlackHorse")) {
    			System.out.println("x2 still has it's original value of " + x2.name.toString() + " 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 
            System.gc();		
    	}
    	@Override
        /* 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(this.name + " successfully garbage collected");
        }
    }
    Output:
    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
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    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
  •