Rookie
Rookie

Reputation: 8790

What will use more memory

I am working on improving the performance of my app. I am confused about which of the following will use more memory: Here sb is StringBuffer

String strWithLink = sb.toString();
clickHereTextview.setText(
     Html.fromHtml(strWithLink.substring(0,strWithLink.indexOf("+"))));

OR

clickHereTextview.setText(
     Html.fromHtml(sb.toString().substring(0,sb.toString().indexOf("+"))));

Upvotes: 1

Views: 213

Answers (6)

aioobe
aioobe

Reputation: 420951

In terms of memory an expression such as

sb.toString().indexOf("+")

has little to no impact as the string will be garbage collected right after evaluation. (To avoid even the temporary memory usage, I would recommend doing

sb.indexOf("+")

instead though.)

However, there's a potential leak involved when you use String.substring. Last time I checked the the substring basically returns a view of the original string, so the original string is still resident in memory.

The workaround is to do

String strWithLink = sb.toString();
... new String(strWithLink.substring(0,strWithLink.indexOf("+"))) ...
    ^^^^^^^^^^

to detach the wanted string, from the original (potentially large) string. Same applies for String.split as discussed over here:

Upvotes: 3

Kshitij
Kshitij

Reputation: 8614

Creating new objects always take up more memory. However, in your case difference seems insignificant.

Also, in your case, you are creating a local variable which takes heap space.

Whenever there are references in more than one location in your method it good to use String strWithLink = sb.toString();, as you can use the same strWithLink everywhere . Otherwise, if there is only one reference, its always better to just use sb.toString(); directly.


Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533492

The less work you do, the more efficient it usually is. In this case, you don't need to call toString at all

clickHereTextview.setText(Html.fromHtml(sb.substring(0, sb.indexOf("+"))));

Upvotes: 1

d1e
d1e

Reputation: 6442

Analysis

If we look at StringBuilder's OpenJDK sources:

public String toString() {
    // Create a copy, don't share the array
    return new String(value, 0, count);
}

We see, that it instantiates a whole new String object. It places in the string pool as many new instances as many times you call sb.toString().

Outcome

Use String strWithLink = sb.toString();, reusing it will retrieve the same instance of String from the pool, rather the new one.

Upvotes: 2

Miquel
Miquel

Reputation: 15675

Check other people's answers, the second one does take a little bit more memory, but this sounds like you are over optimizing. Keeping your code clear and readable should be the priority. I'd suggest you don't worry so much about such tiny optimizations if readability will suffer.

Upvotes: 1

FThompson
FThompson

Reputation: 28687

The second will use more memory, because each call to StringBuilder#toString() creates a new String instance.

http://www.docjar.com/html/api/java/lang/StringBuilder.java.html

Upvotes: 2

Related Questions