tag:blogger.com,1999:blog-8712770457197348465.post7584728779079728866..comments2024-03-18T23:01:42.768-07:00Comments on Javarevisited: How SubString method works in Java - Memory Leak Fixed in JDK 1.7javin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger22125tag:blogger.com,1999:blog-8712770457197348465.post-9336518954438311922014-12-10T04:43:00.836-08:002014-12-10T04:43:00.836-08:00Hi, As you just wrote
In case If you have still n...Hi, As you just wrote<br /><br />In case If you have still not figured it out, If the original string is very long, and has array of size 1GB, no matter how small a substring is, it will hold 1GB array. This will also stop original string to be garbage collected, in case if doesn't have any live reference.<br /><br />I can not understand. Because I think the new string created by substring andyhttps://www.blogger.com/profile/17982244721576930142noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-13501516496053220482014-06-06T10:05:00.567-07:002014-06-06T10:05:00.567-07:00Hey guys, this is Uma. Think this is not quite cor...Hey guys, this is Uma. Think this is not quite correct: -.what will happen if beginIndex is equal to length in substring(int beginIndex),for the answer provided - empty string will be returned. Question ought to have been what will happen if beginIndex is equal to length of string from which you want a piece of. That is what the person who gave this example: "emptiness".substring(9) Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-50415943072507206492014-05-04T04:59:56.070-07:002014-05-04T04:59:56.070-07:00U have mentioned above that " This will also ... U have mentioned above that " This will also stop original string to be garbage collected, in case if doesn't have any live reference.". can you please elaborate it more. I didn't understood it.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-83337443044354836692013-12-11T09:28:09.154-08:002013-12-11T09:28:09.154-08:00SubString will always create a new object, but in ...SubString will always create a new object, but in 6 it was still referring to the 1gb array, even after performing the sub string. In 7 we create a copy of new char array to hold the substring and leave the huge array for garbage collection if not needed. Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-74583874372414414252013-10-20T07:52:07.419-07:002013-10-20T07:52:07.419-07:00Hi,
while debugging we can see that even after su...Hi,<br /><br />while debugging we can see that even after substring it had all the content in the backing array.<br />so if we don't want to keep the same we can use <br /> <br />String new2 = new String(new1.toCharArray());<br /><br />this will copy the actual array to the backing array.<br />I am not sure about what will happen to String 'new1' with data 1 GB.<br /><br />Thanks,<br Anonymoushttps://www.blogger.com/profile/18251661917747336660noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-72530630215899901222013-08-31T14:11:28.783-07:002013-08-31T14:11:28.783-07:00Hey Javin,
I've been reading your articles an...Hey Javin,<br /><br />I've been reading your articles and although they are very helpful. This article does have much content on the topic of sub-string and whatever it does have is quite confusing. Which actually goes for a lot of your articles.<br /><br />On reading this i was under the impression that a brand new string gets created when substring method is called and because of memory Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-53032923861619563862013-08-15T20:40:08.912-07:002013-08-15T20:40:08.912-07:00Indeed substring in Java will not create any more ...Indeed substring in Java will not create any more memory leak from Java Java 1.7.0_06, issue has been fixed by not allowing sharing of char array. They have even made some changes in java.lang.String class to remove count and offset int variable, which can further save some memory, but this comes with a cost, by compromising speed. Earlier substring was O(1) operation because of sharing same Georgenoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-82630448402123801382013-06-27T08:46:11.187-07:002013-06-27T08:46:11.187-07:00@javin, can you please move the update note at the...@javin, can you please move the update note at the top of the article? I fear that readers who will only scan this article will be misinformed.<br /><br /> - MichaelAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-82858338765779285462013-06-06T20:06:37.253-07:002013-06-06T20:06:37.253-07:00how to search for a given pattern in a string with...how to search for a given pattern in a string without using regex?<br />e.g.<br />search "aab" total no of occurances and the index at which this pattern in string "aabcbbacbaabaaaabbabaab"?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-21493209653311016582013-03-09T17:25:19.712-08:002013-03-09T17:25:19.712-08:00@Anonymous, What is your questions? isn't you ...@Anonymous, What is your questions? isn't you answering your own question, if I understood correctly, in "emptiness".substring(9) returns "" (an empty string)Javin @ BlockingQueue in Javahttp://javarevisited.blogspot.com/2012/12/blocking-queue-in-java-example-ArrayBlockingQueue-LinkedBlockingQueue.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-1482069943457740022013-03-09T09:23:08.779-08:002013-03-09T09:23:08.779-08:00Is it really true? "no it won't throw Ind...Is it really true? "no it won't throw Index OutOfBoundException instead it will return empty String, same is the case when beginIndex and endIndex is same in case of second method." <br /><br />Source - http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#substring(int)<br />"<br />substring<br /><br />public String substring(int beginIndex)<br />Returns a new string Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-48296940404433033442013-03-07T03:22:44.660-08:002013-03-07T03:22:44.660-08:00@Yves Gillet, you are correct mate, It seems, subs...@Yves Gillet, you are correct mate, It seems, substring method is now free from memory leak. Will update this post. ThanksJavin @ Java Classloder Workinghttp://javarevisited.blogspot.com/2012/12/how-classloader-works-in-java.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-85393351117843319322013-03-06T07:23:37.078-08:002013-03-06T07:23:37.078-08:00what you are referring to the substring problem wa...what you are referring to the substring problem was in fact a bug:<br />http://bugs.sun.com/view_bug.do?bug_id=6294060<br /><br />I hope interviewers are at least aware that some of their questions are not valid anymore :)Anonymoushttps://www.blogger.com/profile/04947916006924477725noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-30399097401187743312013-03-06T07:20:04.310-08:002013-03-06T07:20:04.310-08:00Yea beware the implementation changed since java 7...Yea beware the implementation changed since java 7 at least, the substring no longer backs up the original char[] array, it creates a new copy of it. Please reflect this in you article.Anonymoushttps://www.blogger.com/profile/04947916006924477725noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-83214229477325458002013-02-15T07:47:51.011-08:002013-02-15T07:47:51.011-08:00What I don't understand is how this 1G array i...What I don't understand is how this 1G array is backed up by calling substring. The substring will call new String(value, beginIndex, subLen) and inside this constructor the following code exists: this.value = Arrays.copyOfRange(value, offset, offset+count). So our five characters are copied into a new array and the original 1G is not backed up.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-18264076344315334582013-01-24T13:22:58.595-08:002013-01-24T13:22:58.595-08:00The String(String) constructor has changed between...The String(String) constructor has changed between Java 6, 7, and (the early access version of) 8. That bit about stripping out the baggage is no longer in there.<br /><br />What you need to do, if your small sub-string of a large string is in variable "name", is:<br /><br />name = new String(name.toCharArray());Dave Conradhttps://www.blogger.com/profile/06688886122549415711noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-11456873773588201192012-10-06T15:33:32.449-07:002012-10-06T15:33:32.449-07:00I was asked exactly this question in a Google tele...I was asked exactly this question in a Google telephone interview. Of course, I failed the last part which assumed that I knew how substring is implemented internally.<br /><br />When my reviewer mentioned this implementation, I told him that this is definitely implementation-dependant and that other JVMs should behave differently.<br /><br />In the end, I passed that interview.papajohnhttps://www.blogger.com/profile/01972028686288569923noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-82889236839685275392012-09-20T15:39:10.690-07:002012-09-20T15:39:10.690-07:00@Apoorv: No array contents are copied. Several St...@Apoorv: No array contents are copied. Several Strings can point at the same character array with different lengths and offsets since this array will never change (which is the whole point of the String class being immutable). What the new String will contain is a pointer to the 1 GB array with a different length and offset.<br /><br />What I assume the interviewer was aiming towards was what Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-77643212317759781312012-06-14T00:05:25.409-07:002012-06-14T00:05:25.409-07:00Regarding the first comment related to "retur...Regarding the first comment related to "return ((beginIndex == 0) && (endIndex == count)) ? this : new String(offset + beginIndex, endIndex - beginIndex, value)". This is correct observation, and in the constructor used while creating a new string in above code of substring, only required array contents are copied and not the entire array. So the assumption that creating a smallApoorv Birthare (AB)https://www.blogger.com/profile/01758404272340603427noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-33186912241250929702012-05-29T07:09:18.084-07:002012-05-29T07:09:18.084-07:00Hi,
I read your article and i cant able to unders...Hi,<br /><br />I read your article and i cant able to understand completely. Why the empty string is returned, when begin index and start index is same, i checked Java api i didn't get clear idea. Why they are doing like this. So i started searching in internet. I found this post. <br /><br />http://www.coderanch.com/t/564948/java/java/does-substring-method-works<br /><br />Can you give more Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-85504163170388373912012-03-22T00:06:14.334-07:002012-03-22T00:06:14.334-07:00I understand how substring works in Java but what ...I understand how substring works in Java but what is point of keeping original character array inside substring object ? Since string is immutable you can not change a String once created and SubString are String.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-663810376678173752011-10-10T06:00:03.931-07:002011-10-10T06:00:03.931-07:00In the substring method of String class, the code ...In the substring method of String class, the code is as follows:<br /><br />return ((beginIndex == 0) && (endIndex == count)) ? this : new String(offset + beginIndex, endIndex - beginIndex, value);<br /><br /><br />The code is returning a new String object which contains the same backing array. As the array is an object reference, the two string objects will be pointing to the same Anonymousnoreply@blogger.com