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!!!

9
  • Does String empid = new String(str.substring(0,2)); accomplish what you want? Commented Nov 8, 2016 at 13:46
  • Could you explain 'creates the same space for original string.so memory leak will happen'? Commented Nov 8, 2016 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. Commented Nov 8, 2016 at 13:49
  • yes i agreed its creates new string but the allocated space for that string is same as original string right? Commented Nov 8, 2016 at 13:52
  • 2
    The memory leak issue for substring was addressed in Java 7. What version of Java are you using? Commented Nov 8, 2016 at 13:52

3 Answers 3

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.
Sign up to request clarification or add additional context in comments.

1 Comment

,thanks for the clear explanation about the substring method.
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.

Comments

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)});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.