Reputation: 10748
I'm programming a java app and trying to educate myself on memory management. If instantiate a String object in a method of an object that's already been created, will that String object exist in memory after the method is executed? Take this example. Does newString exist in memory after i execute myObject.setNewName()? or does java destroy those objects if they are not instance variables?
public class MyObject(){
private String name;
private Integer id;
public myObject(Integer id, String name){
this.id = id;
this.name = name;
}
public void setNewName(){
String newString = "This is a new name";
this.name = newString;
}
}
another file
MyObject myObject = new MyObject(4,"Bob");
myObject.setNewName();
Upvotes: 0
Views: 741
Reputation: 1
It has nothing to do with scope. We could create a an array in a method and have that method return that array to an array variable in main. The scope would change from the method to main and the array would still exist, despite the array going out of scope.
It purely has to do with how memory is deallocated in Java. In contrast to C++, every object in Java is dynamically allocated in heap at creation. This means that objects are not stored in the stack memory allocated for a specific function. Because of this the function could finish executing, the stack memory for that function would be released, and the objects created within the function would still exist in heap memory.
Instead, Java deletes these objects using garbage collection which hinges on reference counting. That is, if there was, for example, an array variable waiting in main for an array created and returned by a function, that array would still be referenced by the array variable back in main, so the garbage collector would not delete the memory used for the array created in the function even if the function finished executing. The garbage collector would wait until that array is no longer referenced to then delete it.
Now, the garbage collector could wipe out that same array created in the function if the function finishes executing and no lvalue variable references it.
Upvotes: 0
Reputation: 7362
When you declare an object it's volatility (and thus the objects contents) will have the lifespan of the method/object that has declared it.
Upvotes: 0
Reputation: 51030
Since, the string literals (e.g. "This is a new name"
) are interned, they will be cached and remain in memory even after the method ends.
But, generally objects are gc'ed once the variable(s) referencing the object go(es) out of scope.
e.g String newString = new String("This is a new name");
, then the object would be eligible for gc once the method returns because newString
would be longer there holding the reference to the object.
Note: That doesn't mean you should use new String("This is a new name")
instead of "This is a new name", because interning happens in both the cases. The difference is that with
new String("This is a new name")` you are creating one extra object explicitly.
Upvotes: 0
Reputation: 834
Funnily enough, the scenario you are asking about overlaps two aspects of Java memory management, meaning your answer is both yes and no.
String newString = "This is a new name";
This line will be held in memory from the first allocation through to the end of the program, as it is referencing a string literal, which is static. The compiler does this because it can guarantee "This is a new name"
will never change, so it's useless CPU cycles to allocate, reallocate and delete it every time the method is run.
On the other hand, if the line was, for example
String newString = new Object().toString();
This is not a literal, and so a new object (which is a String
) is create during each new run of the method. So, when is this object deallocated? You can guarantee it will survive to the end of the method, as it is referenced by newString
. After that, it will survive until the next time the garbage collector runs. However, nothing will have a reference to it any more, so it's impossible to access anyway.
Upvotes: 0
Reputation: 848
As long as you hold a reference to it, the object still will remain in memory. Once the string is not referenced by any object, it will become garbage-collectable, which means the memory will be recovered at some point.
Upvotes: 0
Reputation: 3191
String is actually created in a constant pool,and will be exists through the whole JVM lifecycle.You can check it as follow:
public static void main(String[] args) {
aMethod();
String str1 = "This is a string";
System.out.println(System.identityHashCode(str1));
}
public static void aMethod(){
String str0 = "This is a string";
System.out.println(System.identityHashCode(str0));
}
str0 and str1 is actually the same because their hashcode equals.
Strings are kind of specific objects,If a common object's is no longer referenced by anyone ,It will be GC-ed at sometime and the memory will be recircled.
Upvotes: 1
Reputation: 425198
If the reference to the object is held beyond the method, for example assigned to a field, then the object won't be garbage collected when the method ends.
Objects only assigned to local variables will be "not reachable" when the method ends, so will be marked for garbage collection.
It's all to do with scope. If the reference to the object is still in scope, it will remain in memory.
Upvotes: 2