简体   繁体   English

终结器线程会导致内存不足吗?

[英]Do Finalizer thread can cause Out of Memory?

Finalizer thread are responsible clearing objects in the finalization queue. 终结器线程负责终结队列中的清除对象。 Ironically does the same finalizer thread can responsible for OOM? 具有讽刺意味的是,相同的终结器线程可以负责OOM吗?

Short answer: theoretically yes. 简短答案: 理论上是。

More specifically it depeneds on how your finalizer thread is constructed and what he does. 更具体地说,它取决于终结器线程的构造方式和作用。 In general any new object creation can lead to OOM if no free memory left. 通常,如果没有剩余可用内存,则任何新对象创建都会导致OOM。

The short answer is yes. 简短的答案是肯定的。

Some classes implement the Object.finalize() method. 一些类实现Object.finalize()方法。 Objects which override this method need to called by a background thread call finalizer, and they can't be cleaned up until this happens. 覆盖此方法的对象需要由后台线程调用终结器进行调用,并且在发生这种情况之前无法对其进行清理。 If these tasks are short and you don't discard many of these it all works well. 如果这些任务很短,并且您没有丢弃很多任务,那么一切都很好。 However if you are creating lots of these objects and/or their finalisers take a long time, the queue of objects to be finalised builds up. 但是,如果要创建大量这些对象和/或它们的终结器花费很长时间,则要终结的对象队列会增加。 It is possible for this queue to use up all the memory. 该队列可能会用完所有内存。

If many objects with finalizers are created, it is likely that there will be performance issues, even if the underlying native resources are explicitly freed using try-finalize blocks. 如果创建了许多带有终结器的对象,则即使使用try-finalize块显式释放了基础本机资源,也可能会出现性能问题。

Code to try and outpace java garbage collector with finalizers showed the following result. 尝试使Java垃圾收集器的终结器超过代码的代码显示了以下结果。

It is surprisingly easy to outpace the Java garbage collector if lots of objects with finalizers are created, leading to spurious out-of-memory errors with lots of objects which could theoretically be reclaimed.More on this can be found on the link http://www.enyo.de/fw/notes/java-gc-finalizers.html 这是非常容易超越Java垃圾收集器只有在创造了大量与终结的对象,从而导致虚假出内存不足的错误有大量的对象,其理论上可以reclaimed.More这个可以在链接中找到的http:/ /www.enyo.de/fw/notes/java-gc-finalizers.html

There are some apps that have hit this finalizer queue build up problem in the past, so it is worth considering how to deal with it. 过去有一些应用程序遇到了此终结器队列建立问题,因此值得考虑如何处理。 One obvious way is to increase the priority of the "Finalizer" daemon thread - there is no API for this, so you have to run through all the threads to find it by name, then increase it's priority. 一种明显的方法是增加“ Finalizer”守护程序线程的优先级-没有为此的API,因此您必须遍历所有线程以按名称查找它,然后增加其优先级。

You could also take explicit control over finalization by removing the finalize() method and using your own explicit queue using your own Reference objects in a very similar way that the Finalizer class processes the objects and their finalize() methods . 您也可以通过删除finalize()方法并使用自己的Reference对象使用自己的显式队列来对终结进行显式控制,其方式与Finalizer类处理对象及其finalize()方法非常相似。 That way you control your finalization processing thread's priority and schedule. 这样,您可以控制终结处理线程的优先级和时间表。

Note that neither of these techniques reduce the overheads in having finalizable objects, they just avoid the queue building up because of the lower priority thread. 请注意,这些技术都不能减少拥有可终结对象的开销,它们只是避免了由于优先级较低的线程而导致的队列建立。

How finalization works is shown in the link below for the curious 出于好奇,下面的链接中显示了完成工作的方式

http://www.fasterj.com/articles/finalizer2.shtml http://www.fasterj.com/articles/finalizer2.shtml

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

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