When java passes string to a function it is pass by value not a pass by reference for reference click here .
getKeyValue(String o); // This creates a new string and passed by value
Class A
{
String o;
}
A a;
getKeyValue(A a); // This passes reference of object a, String o is not duplicated.
So when i have a multi level JSON Object like {a:{b:{c:"d"}}} and i have a path from which i need to get the value from like a:b:c (path)
for(String s: path.split(':')
{
jsonObject = jsonObject.get(s)
}
println jsonObject
Assume i have 2 GB String, now when i do jsonObject.get(s) I create 2 GB copy by value and the pass it to the method and gc needs to clear all that memory every time i try to go one step ahead.
Is there any way to avoid this without writing a library to accept the string as a reference using StringBuilder etc.. .
Is there any library which tackles this issue in java.