2

I have a string like below

12SELVASOFTWAREENGINEER................323

This string length is more than 10000

First two letter of this string is employee Id

So i can get my employee id like below

String empid = str.substring(0,2);

but this string creates the same space for original String, so memory leak will happen.

How to avoid this?

Is their any alternative or efficient way is their for getting the part of the string in java

Any help will be greatly appreciated!!!

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
Selva
  • 1,620
  • 3
  • 33
  • 63
  • Does `String empid = new String(str.substring(0,2));` accomplish what you want? – bradimus Nov 08 '16 at 13:46
  • Could you explain 'creates the same space for original string.so memory leak will happen'? – Ridcully Nov 08 '16 at 13:49
  • What memory leak are you talking about? `str.substring(0, 2)` returns a **new** string that is a substring of `str`. The new string will be only 2 characters long. – Klas Lindbäck Nov 08 '16 at 13:49
  • yes i agreed its creates new string but the allocated space for that string is same as original string right? – Selva Nov 08 '16 at 13:52
  • 2
    The memory leak issue for `substring` was addressed in Java 7. What version of Java are you using? – khelwood Nov 08 '16 at 13:52
  • See http://stackoverflow.com/questions/14161050/java-string-substring-method-potential-memory-leak – khelwood Nov 08 '16 at 13:52
  • @khelwood I am using java 1.7.Is it solved? – Selva Nov 08 '16 at 13:53
  • It was fixed in Java 7u6. If you are using any version after that (you probably are), then it isn't a problem. If not, do what @bradimus suggested. – khelwood Nov 08 '16 at 13:54
  • @Khelwood,thanks for the solution.i am using jdk1.7.0_10 version – Selva Nov 08 '16 at 13:56

3 Answers3

5

... but this string creates the same space for original string.so memory leak will happen.

I think you are referring to the behavior of String in older versions of Java where String.substring(...) created an object that shared the original String object's backing array. As you point out, that could lead to a memory leak; e.g. if the original string became unreachable and the substring didn't.

The relevant code can be seen here:

This problem was fixed in Java 7.

Prior to Java 7, the standard workaround was to do something like this:

   String empid = new String(str.substring(0, 2)); 

(The new String(...) creates a string that does not share its backing array with either the original string or the substring.)

However, this is only advisable if:

  • you are using an old release of Java,
  • the original string is likely to be large, and
  • the substring is likely to be long-lived.
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

If you intend to keep in memory your original String, using str.substring(0,2) can still be used as it will only reuse the backing array of your original String so it won't affect your memory footprint.

However if you intend to get rid of your original String and want to consume less memory, then you could build a new String instance from the first (charAt(0)) and the second (charAt(1)) character of your original String as next:

String sub = new String(new char[]{s.charAt(0), s.charAt(1)});
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
1

This should work perfectly well:

String orig = "12SELVASOFTWAREENGINEER................323";

String empId = orig.substring(0, 2);

Why should there be a memory leak?

If you mean the memory that's held by orig, you can reset orig afterwards like so:

orig = null;

This will allow the long string to be collected by the garbage collector. The empId will still keep its value - in case you might fear that it gets cleared as well.

Ridcully
  • 23,362
  • 7
  • 71
  • 86