简体   繁体   中英

memory management in java strings?

I have created strings object as given below

Case 1:

  String s1="mystring";//Address location is 1000
  String s1="yourstring";
  String s2="mystring";

Case 2:

  String s1=new String("mystring");//Address location is 1000
  String s1=new String("yourstring");
  String s2=new String("mystring");

with ref .

As per my knowledge these string are stored in string constant pool for Case1 with specific memory locations, similarly in case2 objects are created in Heap memory. How does these memory will be managed and freed by garbage collector if we assign same or different strings to String objects.

If you're coming from C++, everything is on the heap. Also, Strings are immutable in Java (cannot be changed after it is created). Nothing needs to be freed explicitly since there is a garbage collector.

the application will be depending upon the current memory allotted to it initially.If you create a new string , it will allocate a memory with the size of the string.If there is no enough memory, it will throw error as your heap has been exhausted.it is Garbage Collector that is freeing up the memory once the object does not have any reference.You do not have any control over it.

But at the moment, your current piece of code will not compile with the error in creating String :)

strings are stored in string constant pool for Case1(literal strings). In case 2, even if you create string using new object, the object's contents will be checked. If it equals to literal string object's contents then object reference would be pointed to literal string object.

Garbage Collection : If no object reference points to string literal object "mystring"(in your cases) then it will not be still garbage collected, these object will remain until program life time. String literal object will be deleted when program ends. For other string objects ie using keyword new, those will be garbage collected when no object reference points to that object.

As far as my understanding of Java goes, in the first form (ie, by using the = operator), the string is assigned from a pool of Strings maintained by Java. So in the first case, when you assign "mystring" to the variable s2 , it is assigned the reference to the same String that existed when s1 was created initially.

But in case of using the constructor, you create a whole new instance of the String object. So there are now three references to three different Strings.

As far as the memory is concerned, Java implements a Garbage Collector as a Daemon Thread which runs as long as the JVM is in action. It will remove all the objects to which there are no references. So in the first case, there will be no Garbage Collection, but in the second case, one string will be Garbage Collected.

I think you're thinking of string interning. However, since both cases involve string literals, you will see string interning in both cases. Case 2 certainly isn't a memory optimisation over case 1.

Have a look at a similar question, What is the purpose of the expression "new String(...)" in Java?

Similar to another post - this will give you better idea and clarification What's the difference between String s = "something"; and String s = new String("something");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM