1

What is more memory efficient? How much different does it make?

Properties properties = getProperties();
doSomethingWithProperties(properties);

or

doSomethingWithProperties(getProperties());

I have learned that the last one is more memory efficient. But I can't find any documentation on it.

I'm using java 8

  • 5
    The only difference between the two is whether or not a local variable exists afterwards. There will be basically 0 real-world performance difference between those two. – Joachim Sauer Jan 25 '23 at 09:57
  • 2
    You will have to make your own [benchmark](https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) if you need to be sure. I would expect the compiler to optimize away any difference, nothing I know. – Ole V.V. Jan 25 '23 at 10:14
  • [Compiler Explorer backs up @JoachimSauer's comment](https://godbolt.org/z/fevzvjc6j) - the only difference between the two is the extra store/load instruction. I would be incredibly surprised if the JVM couldn't optimize that away, and even if it can't, I doubt there's any meaningful performance difference. – Joe Clay Jan 25 '23 at 10:21
  • 1
    @JoeClay: since basically all optimizations exist in the JVM itself and the Java compiler (`javac`) does basically none, looking at the bytecode to guesstimate performance impact of any change is almost pointless and I'd even argue highly misleading. I agree that the JVM will almost certainly optimize that away, though. – Joachim Sauer Jan 25 '23 at 10:22
  • @user16320675 Please read the rest of the comments. The bytecode will be optimized by the JVM at runtime. – m0skit0 Jan 25 '23 at 10:36
  • @user16320675 Specifically this part "and an additional stack position for the variable". Not always true depending on the JVM implementation as discussed on the previous comments. – m0skit0 Jan 25 '23 at 11:14
  • 1
    @m0skit0 which JVM does change/optimize the bytecode? JIT compiler, AFAIK, does not change the bytecode. (My comment is based in part on the JVM Specification [2.6.1. Local Variables](https://docs.oracle.com/javase/specs/jvms/se19/html/jvms-2.html#jvms-2.6.1)) (( sure a stack position can be (re-)used by different local variables {not in posted code} )) BTW maybe you should re-read the comment - nowhere did Joachim state that the JVM does bytecode optimization! – user16320675 Jan 25 '23 at 13:07
  • @JoachimSauer Do you have any source to back your statement? – Martijn Jan Jaap de Bruin Jan 25 '23 at 13:45
  • @MartijnJanJaapdeBruin: it's hard to get **explicit and concrete** numbers for *vague and general* questions. If you want concrete values, then [write a microbenchmark](https://stackoverflow.com/q/504103) for a specific scenario you care about. There's other questions discussing this: [question 1](https://stackoverflow.com/q/1923795), [question 2](https://stackoverflow.com/q/62913400), [question 3](https://stackoverflow.com/q/41696366), – Joachim Sauer Jan 25 '23 at 13:49
  • Thanks for the links, those help – Martijn Jan Jaap de Bruin Jan 25 '23 at 14:02
  • 2
    It's not concrete because the bytecode that `javac` produces and the actual code that the JVM executes can be extremely different. We are talking about optimization because those optimizations decide if there is even any relevant difference between the two. And benchmarks can check memory usage as well. My answer stands: since the local variable is extremely likely to be optimized away there will be 0 observable difference between the two. The usual caveats of Java runtime optimizations (too numerous to list here) do apply. – Joachim Sauer Jan 25 '23 at 14:06
  • 2
    In case of the HotSpot JVM, the c2 compiler will convert the code to [SSA](https://en.wikipedia.org/wiki/Static_single-assignment_form) before starting the optimizations. In this form, both code variants are identical. – Holger Jan 26 '23 at 09:10

0 Answers0