Created
September 3, 2017 17:28
-
-
Save LazaroIbanez/301d9f81070363c4ce7f50a1eb5ea70f to your computer and use it in GitHub Desktop.
Java Garbage collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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