0

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.

Prabhath
  • 629
  • 6
  • 13
  • 1
    *Everything* in Java uses pass-by-value... it's not really clear to me what you're asking, to be honest. The first half of your question with the `getKeyValue` method and `Class A` is particularly unclear :( – Jon Skeet Mar 15 '16 at 13:25
  • When you pass a string in Java, it's not duplicated. It's only duplicated when you try to modify it, since Java strings are immutable and to modify them you create a new instance. – matanso Mar 15 '16 at 13:29
  • @JonSkeet True. but when a is passed "the reference is copied as a value" not the entire object . check this http://javapapers.com/core-java/java-pass-by-value-and-pass-by-reference/ – Prabhath Mar 15 '16 at 13:32
  • @matanso No when a string is passed, it is duplicated (pass by value) check this link http://www.programcreek.com/2013/09/string-is-passed-by-reference-in-java/ . Though when the function returns GC is supposed clear all that memory, i am trying to avoid this – Prabhath Mar 15 '16 at 13:35
  • 3
    `getKeyValue(String o)` will pass the reference to `o` by value. This means that it is not the string itself being copied, but its reference (i.e. something similar to a memory address). – francesco foresti Mar 15 '16 at 13:36
  • In java, a [`String` is an `Object`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html). References to `String`s are passed by value. The content is neither passed nor duplicated, just like all other objects. – bradimus Mar 15 '16 at 13:37
  • @Prabhath: I'm very aware of how references work in Java. (And no, strings aren't "duplicated".) What's not clear to me is what you're asking. – Jon Skeet Mar 15 '16 at 13:44
  • @JonSkeet they are duplicated read about it here search for "Passing Strings" at http://www.cs.utoronto.ca/~dianeh/tutorials/params/ – Prabhath Mar 15 '16 at 13:57
  • @JonSkeet after so many qualified people saying this . I am bit confused right now. can you give me a source for me to understand how a string is passed to a function without duplicated and passed by value. – Prabhath Mar 15 '16 at 14:02
  • read http://javadude.com/articles/passbyvalue.htm especially the part "now the details" and "On pointers versus references" – francesco foresti Mar 15 '16 at 14:03
  • @Prabhath: The *reference* is copied. That does *not* duplicate the string object at all. (That Toronto paper is really bad, btw, given that every time the same string constant is used, you'll get a reference to the same object, contrary to its claims.) See http://stackoverflow.com/questions/40480 – Jon Skeet Mar 15 '16 at 14:04
  • @JonSkeet I agree what is written at javadude. This is the same I was trying to mention with class A containing string and getting passed as a reference. Try to explain why there is a difference between http://ideone.com/KpICsn and http://ideone.com/b2zPMV – Prabhath Mar 15 '16 at 14:34
  • Your two pieces of code are doing different things. If you change the second one to `a = new A("Inside method")` you'll see that that doesn't change the variable in `main`. You're making a false comparison, basically. – Jon Skeet Mar 15 '16 at 14:35

1 Answers1

0

If you have to deal with a 2GB String, maybe a better approach would be to use an InputStream to feed the json parser. Assuming you are using Jackson and the json you are reading comes from a file (say a.json), you could do something like the following :

ObjectMapper objectMapper = new ObjectMapper();
InputStream input = new FileInputStream("data/a.json");
A myA = objectMapper.readValue(input, A.class);

This approach could (and I repeat : could) be more memory efficient than reading the entire string into memory before parsing it. With respect to the 'pass by value' String duplication : no string is duplicated if you pass it to a method (just the reference to the string is duplicated), so no memory problem there.

francesco foresti
  • 2,004
  • 20
  • 24