Memory efficient way to initialize a string to be reused inside a loop

I'm using a couple of lines in my code to be reused in a loop, and I'm wondering what would be the best way to initialize String variables to improve memory usage:

// Just for sample purposes I will declare a Map, but the same thing
// applies for an ArrayList, Database Set, etc. You get the point.
Map<String, String> sampleMap = getMap();
int mapSize = sampleMap.size();

// String initialization
String a;
String b = new String();
String c = "";

for(int i = 0; i < mapSize; i++){
    a = sampleMap.get(i);
    b = someFunc(a);
    c = anotherFunc(a);
    // Do stuff with all the Strings
}

      

After looping strings Strings are no longer used.

+3


source to share


4 answers


You cannot optimize initialization or memory usage of lines in your code snippet for several reasons:

  • Java strings are immutable - you cannot change characters within a string, but you can point to a newline.

  • In your loop, the called function (for example, aFunc()

    or get()

    ) is responsible for highlighting the line, not the loop.



For advanced programmers: if you want to optimize the use of strings within strings of memory, you will need to use a StringBuffer / StringBuilder or raw character array and pass them around the various function calls. You have to create a buffer before the loop and pass it to get()

and other functions so that they use that one buffer instead of allocating and returning their own.

+6


source


Reduce the scope of your variables to restrict them to where they are used:

// Just for sample purposes I will declare a Map, but the same thing
// applies for an ArrayList, Database Set, etc. You get the point.
Map<String, String> sampleMap = getMap();
int mapSize = sampleMap.size();

for(int i = 0; i < mapSize; i++){
    String a = aFunc();
    String b = sampleMap.get(i);
    String c = anotherFunc();
    // Do stuff with the Strings
}

      



There is no performance advantage when declaring variables outside of the loop.

+7


source


Since you are not adding the String with another, the strings are already in memory (because they are immutable), I think this will not affect your memory footprint.

+1


source


I am using a couple of lines in my code to be reused in a loop

No, it is not. You are using multiple string variables that will be reused in the loop. There are no actual lines. Here's the loop body you posted:

a = sampleMap.get(i);
b = someFunc(a);
c = anotherFunc(a);

      

Values a

, b

and c

from previous iterations (or values ​​before the first iteration) are completely ignored inside the loop body. Any value you assign before the loop is irrelevant at best, and distracting and / or expensive at worst. (If you use an empty string, and for some reason none of the other VMs ever use an empty string, you enter it for no reason.)

I would recommend following 6ton's guidelines and introducing variables in a loop to begin with, for readability - it won't affect your memory efficiency.

+1


source







All Articles