简体   繁体   English

在Java中,新旧本地之间是否存在性能差异?

[英]In Java, is there a performance difference between new and local?

In C and C++ I know that there could be a huge difference in performance between instantiating objects on the stack vs. using 'new' to create them on the heap. 在C和C ++中,我知道在堆栈上实例化对象与使用“new”在堆上创建对象之间的性能可能会有很大差异。

Is this the same in Java? 这在Java中是一样的吗?

The 'new' operator in Java is very convenient (especially when I don't have to remember freeing/deleting the objects created with 'new'), but does this mean that I can go wild with 'new'? Java中的“new”运算符非常方便(特别是当我不必记住释放/删除使用“new”创建的对象时),但这是否意味着我可以疯狂地使用'new'?

Erm, there is no other way in java to instantiate an object. 嗯,java中没有其他方法来实例化对象。

All objects are created with new , and all objects are created on the heap. 使用new创建所有对象,并在堆上创建所有对象。

in Java, when you say 在Java中,当你说

MyObject foo;

You're simply declaring a variable (reference). 你只是声明一个变量(引用)。 It isn't instantiated until you say 直到你说,它才会被实例化

foo = new MyObject();

When all references to that object are out of scope, the object becomes elegible for garbage collection. 当对该对象的所有引用都超出范围时,该对象变得易于垃圾收集。 You'll note there's no such thing as delete in java :) 你会注意到在java中没有delete这样的东西:)

There is no allocation of objects on the stack in Java. Java中没有在堆栈上分配对象。

Only local variables (and parameters) can live on the stack and those can only contain references or primitive values, but never objects. 只有局部变量(和参数)可以存在于堆栈中,并且那些只能包含引用或原始值,但绝不能包含对象。

You can't create objects on the stack, you can only have primitives and references on the stack, so the question doesn't apply to Java. 您无法在堆栈上创建对象,您只能在堆栈上具有基元和引用,因此该问题不适用于Java。

There have been attempts to use escape analysis to optimise objects which are short lived (and possibly put them on the stack instead) however I haven't seen any evidence this improved performance. 已经尝试使用转义分析来优化短暂的对象(并且可能将它们放在堆栈上)但是我没有看到任何证据表明这种改进的性能。

Part of the reason there isn't the same performance hit/benifit as there would be in C/C++ is that Java has thread local allocation on the heap and objects are not recycled as agressively. 与C / C ++不同的性能命中/好处的部分原因是Java在堆上具有线程本地分配,并且对象不会像往常一样被回收。 C/C++ has thread local stacks, but you need additional libraires to support multi-thread object allocation. C / C ++具有线程本地堆栈,但您需要其他库以支持多线程对象分配。 Objects are recycled more aggresively which increases the cost of object allocation. 物体被更具侵略性地回收,这增加了物体分配的成本。

One of the biggest changes coming from C/C++ world is to find that Java has far less features, but tries to do make the most of them (There is alot of complex optimisation going on in the JVM) On the other hand Java has a rich/baffling array of open sources libraries. 来自C / C ++世界的最大变化之一是发现Java具有更少的功能,但尝试充分利用它们(JVM中有很多复杂的优化)另一方面Java有一个丰富/令人困惑的开源库。

Repeat after me: there is no allocation of objects on the stack in Java 在我之后重复: 在Java中没有在堆栈上分配对象

In Java, unlike C++, all objects are allocated on the heap, and the only way out is when they are garbage collected. 在Java中,与C ++不同,所有对象都在堆上分配,唯一的出路是它们被垃圾回收。

In Java, unlike C++, the variable falling out of scope does not mean that the destructor of the object runs; 在Java中,与C ++不同, 变量超出范围并不意味着对象的析构函数运行; in fact, there is no destructor. 事实上,没有析构函数。 So the variable might fall out of scope, but the object remains alive on the heap. 因此变量可能超出范围,但对象在堆上保持活动状态。

Can I go wild with 'new'? 我可以和'新'一起疯狂吗?

Yes. 是。 First, because it's the only way to instantiate an object. 首先,因为它是实例化对象的唯一方法。 Second, because the JVM is so good it can create up to 2^32 ightweight objects in less than a second. 其次,因为JVM非常好,它可以在不到一秒的时间内创建多达2 ^ 32个重量级的对象。

在Java中,没有办法在堆栈上手动分配对象,尽管编译器可能决定在堆栈上分配使用'new'创建的对象,请参阅Java理论和实践:重新审视城市性能图例

There's really nothing to compare here: you can't create objects on the stack in Java. 这里真的没什么可比的:你不能用Java在堆栈上创建对象。

If it's any comfort, however, heap-based allocation in Java is (at least usually) quite fast. 如果它的任何安慰,然而,在基于Java的堆分配 (至少通常)相当快。 Java's garbage collector periodically "cleans up" the heap, so it basically looks a lot like a stack, and allocating from it is a lot like allocating from a stack as well -- in a typical case, you have a pointer to the beginning (or end) of the free memory area, and allocating a chunk of memory simply means adding (or subtracting) the amount from that pointer, and returning the address of the beginning (then, of course, constructing an object (or objects) in that area, etc.) Java的垃圾收集器定期“清理”堆,因此它基本上看起来很像堆栈,从它分配就像分配堆栈一样 - 在典型的情况下,你有一个指向开头的指针(或者结束一个空闲的内存区域,并且分配一块内存只是意味着从该指针添加(或减去)数量,并返回开头的地址(当然,然后在那里构造一个或多个对象)地区等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在jsp中编写java之间有性能差异吗? - Is there a performance difference between writing java inside jsp? Java中ArrayList和LinkedList之间的区别 - 性能的原因 - Difference between ArrayList and LinkedList in Java - the whys for performance 测量 Python 和 Java 实现之间的性能差异? - Measuring performance difference between Python and Java implementations? 在Java中==和&gt;或&lt;操作之间是否存在性能差异? - Is there a performance difference between == and > or < operations in java specifically Serializable和Externalizable(Java)之间的性能差异 - Performance difference between Serializable and Externalizable (Java) java 中的 String[]::new 和 new String[] 有区别吗? - Is there a difference between String[]::new and new String[] in java? java 中的 new String[]{} 和 new String[] 之间的区别 - difference between new String[]{} and new String[] in java Java中的局部变量和实例变量有什么区别? - What is the difference between local and instance variables in Java? new type [0]和null之间的区别 - java - difference between new type[0] and null - java 捕获异常或某些类型异常之间是否有任何性能差异? - Java - Is there any performance difference between catching Exception or certain type exception ? - Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM