Java Challenge #3: Garbage Collector

Good challenge! How many objects will be eligible for the garbage collector after the executeAction() invocation?

Try to solve this challenge before seeing the answer below.

public class GarbageCollectorTest {

    public static void main(String[] args) {
	HowManyObjectsWillBeCleanedByTheGarbageCollector gc = new GarbageCollectorTest()
		.new HowManyObjectsWillBeCleanedByTheGarbageCollector();

	gc.executeAction();

	System.out.println("How many objects are eligible be collected by "
		+ "the Garbage Collector after "
		+ "the executeAction() method invokation?" + gc);
    }

    private class HowManyObjectsWillBeCleanedByTheGarbageCollector {
	private Character homer = new Character("Dooh");
	private Character misterBurns = new Character("you are fired");
	private Character bart = new Character("eat my shorts");
	private Character elBarto;

	public void executeAction() {
	    homer.talk();
	    misterBurns.talk();
	    makeTwoCharactersTalk(misterBurns, homer);

	    misterBurns = null;

	    elBarto = bart;
	    bart = null;

	    elBarto.talk();
	}

	private void makeTwoCharactersTalk(Character misterBurns, Character homer) {
	    misterBurns.talk();
	    homer.talk();

	    misterBurns = null;
	    homer = null;
	}
    }

    // class Character omitted
}

class Character {
    private String talk;

    public Character(String talk) {
	this.talk = talk;
    }

    public void talk() {
	System.out.println(this.talk);
    }
}

Answer: There will be one object, misterBurns! When we invoke the makeTwoCharactersTalk we pass the objects by reference and not by value. This means the object is still in the Heap. We also pass the reference of bart to elBarto. When we pass null to bart, we still have the object of Bart into elBarto.

There was a discussion about this challenge with the String in misterBurns object. Developers were saying that this String should be collected by the garbage collector as well. This would be true if the String was being used as new String(); but we are using the String as literal, that means that we are assigning a value directly to the String:

 

public Character(String talk) { this.talk = talk; }

 

By doing that, the String will be in the String pool, therefore it won’t be eligible to the garbage collector.

If you want more details about Strings and garbage collector, you can check this link:
Code Ranch Confusion-String-object-String-Pool

 

Written by
Rafael del Nero
Join the discussion

2 comments
  • Olá Rafael desculpa insistir mas não fiquei 100% esclarecido.
    É claro para mim que dos objectos referenciados pela instancia HowManyObjectsWillBeCleanedByTheGarbageCollector só o “misterBurns” fica eligível para GC.
    Mas este objeto do tipo “Character” contem uma referencia para outro objeto, “talk”, do tipo String.
    Visto que o objeto misterBurns é eligível para GC, o objeto “talk” referenciado apenas pelo objeto “misterBurns” também é eligivel para GC, oou estou a dizer algo de errado?
    Abraço

    • Hi Joel, good catch! Here you are the final answer!

      With the String in misterBurns object. Developers were saying that this String should be collected by the garbage collector as well. This would be true if the String was being used as new String(); but we are using the String as literal, that means that we are assigning a value directly to the String:

      public Character(String talk) { this.talk = talk; }

      By doing that, the String will be in the String pool, therefore it won’t be eligible to the garbage collector.

      Thank you for your comment and keep the code on!

Stand Out as a Java Developer and Get Your Dream Job!

You will get the book by email in a few minutes after registering.