java new String(byte)当参数和String引用当参数有什么区别,为什么性能差别很大?
ringa_lee
ringa_lee 2017-04-18 09:55:19
0
1
671
byte[] test = (sb.toString()).getBytes();
testString(new String(test));

相对于:

byte[] test = (sb.toString()).getBytes();
String temp = new String(test);
testString(temp);

这样有什么区别?为什么第一种比第二种性能方面低很多,而且会有莫名其妙的异常。出问题经常的由于byte[]数组比较大。小的时候看不出来问题。

ringa_lee
ringa_lee

ringa_lee

reply all(1)
Ty80

If you don’t know anything about JVM, it is recommended not to talk about performance, and performance is not measured this way.

The parameters and local variables of the method correspond to the local variable table 3 in the virtual machine stack 1stack frame2. The reference type occupies one slot SLOT, so the difference in your second way of writing is that the stack frame has one more slot. Create After the String object, the reference of the object is on the operand stack String对象后,对象的引用在操作数栈4上,通过aload指令保存到本地变量表,调用testString方法的时候通过aload4

and is saved to the local variable table through the aload instruction. When calling the testString method, pass aload instruction is pushed to the top of the operand stack.

Summary: There is one more SLOT in the stack frame and two more instructions in the method call. The redundant instructions will be eliminated after JIT warm-up. There is no actual difference.
  1. 2.5.2. Java Virtual Machine Stacks ↩
  2. 2.6. Frames ↩
  3. 2.6.1. Local Variables ↩
  4. 2.6.2. Operand Stacks ↩
🎜
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template