byte[] test = (sb.toString()).getBytes();
testString(new String(test));
相对于:
byte[] test = (sb.toString()).getBytes();
String temp = new String(test);
testString(temp);
这样有什么区别?为什么第一种比第二种性能方面低很多,而且会有莫名其妙的异常。出问题经常的由于byte[]数组比较大。小的时候看不出来问题。
PHP中文网2017-04-18 09:57:19
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方法的时候通过aload
4
aload
instruction. When calling the testString method, pass aload< The /code> 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.
-
2.5.2. Java Virtual Machine Stacks ↩
-
2.6. Frames ↩
-
2.6.1. Local Variables ↩
-
2.6.2. Operand Stacks ↩
🎜reply0