简体   繁体   English

有效的Java项目7:避免使用终结器

[英]Effective Java Item 7: Avoid Finalizers

In this amazing book the author Josh Bloch mentions: 在这本惊人的书中,作者乔什·布洛赫(Josh Bloch)提到:

"Oh, and one more thing: there is a severe performance penalty for using finalizers. On my machine, the time to create and destroy a simple object is about 5.6 ns. Adding a finalizer increases the time to 2,400 ns. In other words, it is about 430 times slower to create and destroy objects with finalizers." “哦,还有一件事:使用终结器会严重影响性能。在我的机器上, 创建和销毁简单对象的时间约为5.6 ns。添加终结器会使时间增加到2400 ns。换句话说,使用终结器创建和销毁对象的速度要慢430倍。”

Is there a way we can delete and object in java? 有没有一种方法可以删除Java中的对象?
I thought we can simply let the objects fall out of scope or reset them to null. 我以为我们可以简单地让对象超出范围或将它们重置为null。
I intend to experiment this on my machine , seems like a fun idea but I am not sure how to delete and object. 我打算在我的机器上进行实验,这似乎是一个有趣的主意,但是我不确定如何删除和反对。

一旦您使引用变量引用了null (假设最后一个引用),并且该变量超出了其范围,则该对象可以在下一个垃圾回收周期进行垃圾回收。

An object will cease to exist when there are no longer any strong rooted references to it; 当不再有牢固的根源引用时,该对象将不复存在。 in most cases that's exactly what should happen. 在大多数情况下,这正是应该发生的情况。 In some cases, however, an object will ask an outside entity to do something on its behalf, possibly to the detriment of other entities, in exchange for a promise to let that other entity know when its services are no longer required. 但是,在某些情况下,对象会要求外部实体代表它做某事,这可能会损害其他实体,以换取一个让对方知道何时不再需要其服务的承诺。 For example, a "File" object might ask the OS for exclusive access to a file; 例如,“文件”对象可能会要求操作系统对文件进行独占访问。 until the OS is told that such access is no longer required, it will block everyone else's ability to use that file. 直到操作系统被告知不再需要这种访问,否则它将阻止其他任何人使用该文件。

If an object which had made such a promise were abandoned and simply ceased to exist, the outside entity would keep on doing whatever it had been asked to do, to the detriment of everyone else, even though its actions were no longer of any benefit to anyone. 如果做出这样承诺的对象被放弃并只是不复存在,则外部实体将继续按照要求执行任何操作,从而损害其他所有人,即使其行为已不再有任何益处。任何人。 To avoid this situation, Java allows objects to request notification when the GC notices that they seem to have been abandoned. 为了避免这种情况,Java允许对象在GC注意到对象似乎已被放弃时请求通知。 Such notifications will be given (ie Finalize will be called on such objects) before the objects cease to exist, but there's no real guarantee of timeliness beyond that. 此类通知将在对象不再存在之前给出(即,将在此类对象上调用Finalize ),但没有真正的保证。 An object which is finalized can then notify any and all entities acting on its behalf that they should stop doing so. 然后,已终结的对象可以通知代表它行事的所有实体,他们应该停止这样做。

The creators of Java may have expected finalizers to be the primary mechanism by which objects could notify outside entities that their services are no longer required, but finalization really doesn't work very well. Java的创建者可能希望终结器成为主要的机制,通过这种机制,对象可以通知外部实体不再需要其服务,但是终结处理的效果确实不好。 Other mechanisms such as AutoCloseable or PhantomReference are better in many cases. 在许多情况下,其他机制(如AutoCloseablePhantomReference更好。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM