I made a little code to use as an example:
public class Main {
public static void main(String[] args) {
String hugeString = "a,a,a,a";
splitOutside(hugeString.split(","));
splitInside(hugeString);
}
private static void splitInside(String string) {
String splitData[] = string.split(",");
for (int i = 0; i < splitData.length; i++){
System.out.print(splitData[i]);
}
System.out.println("");
}
public static void splitOutside(String[] splitData) {
for (int i = 0; i < splitData.length; i++){
System.out.print(splitData[i]);
}
System.out.println("");
}
}
I have two different functions, splitOutside
and splitInside
, in this case.
Is any of these better than the other in terms of memory usage? Will splitting the string inside the function help the Garbage collector?
As I said, this is just a tiny example, the real code has huge strings which need to be splitted, and it receives many of these strins each second. So the difference might be noticeable in the long run (it's a software that needs to be ON for periods of over 100 hours).
Will it make a difference using one method or the other?
Update:
This question is not the same as java how expensive is a method call.
I am not asking if the call to a method is expensive, I am asking if the call to String.split();
inside or outside of the function makes any difference.
Update 2:
What if I have this? Will it be different?
while ((hugeString = br.readLine()) != null) {
splitOutside(hugeString.split(","));
splitInside(hugeString);
}
I forgot to mention I am constantly reading from outside the JVM in an (almost) endless loop (the split will happen 2-10 times a second).