4

I have some functionality where I have to pass Strings between functions. The problem is these Strings can get quite big and Strings are passed by value not by reference. If you have a big String, the memory impact is doubled for a method invocation.

Anyone got a tip around this other than don't pass big Strings into methods?

5
  • 1
    It would be really helpful if you posted some code to have a better idea of what you're trying to do. It doesn't sound like you're passing these strings from a VF page to a Controller Class, so the usage would be especially helpful.
    – crmprogdev
    May 7, 2013 at 14:54
  • Can you wrap the string in a plain object and operate on that? May 7, 2013 at 15:04
  • @user320 No because passing any String to that object is by value so it is the same problem. May 7, 2013 at 15:48
  • 2
    @MoreThanFive It would seem that you will not have the same problem, because the string will be added to the object just once, and found in each method after that by using the value of the reference to the object.
    – ipavlic
    May 7, 2013 at 15:58
  • 1
    strings are objects not primitives, so copy by value should just copy the reference, not the actual string. can you post somecode to demonstrate the problem ?
    – superfell
    May 8, 2013 at 14:55

2 Answers 2

6

Apex inherits many language features from Java, and Java passes everything by value (the object references are also passed by copying their values).

String is an immutable class, so mutating instances will always create additional copies. Unfortunately, Apex does not offer a StringBuilder class that would help with some pain.

If it is just passing around that you are concerned with (and not modifying it), then user320's suggestion will work fine. Wrapping a String inside an object (or in a array) will allow you to pass the reference to that object by value. That will save a lot of memory if you need just read-only access because only the wrapping operation might copy the String, and each subsequent passing will not copy the String.

3
  • Java passes by value are you sure? IT doesn't. You .clone() the object to avoid pass by reference. May 7, 2013 at 16:10
  • Java does in fact pass by value, javadude.com/articles/passbyvalue.htm in a very literal sense the object reference that you have as your variable is passed as it's literal value: a reference. The net effect is it acts like pass by ref with objects but it is, strictly speaking, pass by value. May 7, 2013 at 17:18
  • 2
    Java passes everything by value, copying the function argument in a function call. If you have a primitive type p, then calling f(p) will pass a copy of p, p' to function f. If you have a reference type r pointing to an object o, r->o, then it will again be copied into a copy r' which points to the same object r'->o and passed to the function f. The effect is that you can manipulate the object o through either r or r', but r' is still a copy. Cloning the object will produce a copy o' which is separate from object o and is pointed to by some reference r2.
    – ipavlic
    May 7, 2013 at 21:50
1

The solution to this problem is if you have a big String to never ever ever ever pass it to any method. Make it a private variable and then have methods update it.

public class MyClass {
   private static String mybigFatString = ...;

   public void static addTinyStringToBigString(String tinyString) {
       mybigFatString += tinyString;
   }

}

You are effectively treating the big fat String as a singleton. It is never copied, passed, you name it.

1
  • Won't that fail as soon as you have to use substring, left etc methods? Also - if strings are really immutable as @ipavlic writes, there will be a brief moment where you have bigString and bigString + something in memory and you'd run out of the heap space...
    – eyescream
    May 8, 2013 at 21:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .