Skip to content

Instantly share code, notes, and snippets.

@LazaroIbanez
Created September 3, 2017 17:28
Show Gist options
  • Save LazaroIbanez/301d9f81070363c4ce7f50a1eb5ea70f to your computer and use it in GitHub Desktop.
Save LazaroIbanez/301d9f81070363c4ce7f50a1eb5ea70f to your computer and use it in GitHub Desktop.
Java Garbage collection
public class TestClass {
public Object getObject() {
Object object1 = new String(“New_String”);
Object objectArray[] = new Object[1];
objectArray[0] = object1; //After this line, object1 and objectArray[0] are pointing to the same String object.
object1 = null; //After this line, object1 points to null but objectArray[0] is still pointing to the String object.
objectArray[0] = null; //After this line objectArray[0] also starts pointing to null so there is no reference left that is pointing to the String object.
//So it is now available for Garbage collection.
return object1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment