热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

java内存泄漏的故障排除:终结?-Troubleshootingajavamemoryleak:finalization?

Ihaveamisbehavingapplicationthatseemstoleak.Afterabriefprofilerinvestigation,mostmemo

I have a misbehaving application that seems to leak. After a brief profiler investigation, most memory (80%) is held by java.lang.ref.Finalizer instances. I suspect that finalizers fail to run.

我有一个行为不当的应用程序,似乎泄露了。经过简短的分析器调查,大多数内存(80%)由java.lang.ref保存。终结器实例。我怀疑终结器不能运行。

A common cause of this seems to be exceptions thrown from the finalizer. However, the javadoc for the finalize method of the Object class (see here for instance) seems to contradict itself: it states

一个常见的原因似乎是终结器抛出的异常。但是,对象类的finalize方法的javadoc(例如在这里看到)似乎自相矛盾:它声明

If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.

如果finalize方法抛出未捕获的异常,则会忽略该异常,该对象的终结。

but later, it also states that

但后来,它也指出了这一点

Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.

finalize方法抛出的任何异常都会导致该对象的终结,但在其他情况下会被忽略。

What should I believe (i.e., is finalization halted or not?), and do you have any tips on how to investigate such apparent leaks?

我应该相信什么?你对如何调查这些明显的泄漏有什么建议吗?

Thanks

谢谢

5 个解决方案

#1


6  

My first step would be to establish whether this is a genuine memory leak or not.

我的第一步是确定这是否是一个真正的内存泄漏。

The points raised in the previous answers all relate to the speed at which objects are collected, not the question of whether your objects are collected at all. Only the latter is a genuine memory leak.

在前面的回答中提出的要点都与收集对象的速度有关,而不是您的对象是否被收集的问题。只有后者才是真正的内存泄漏。

We had a similar predicament on my project, and ran the application in "slow motion" mode to figure out if we had a real leak. We were able to do this by slowing down the stream of input data.

在我的项目中,我们遇到了类似的困境,并以“慢动作”模式运行应用程序,以确定是否出现了真正的泄漏。我们可以通过减慢输入数据流来实现这一点。

If the problem disappears when you run in "slow motion" mode, then the problem is probably one of the ones suggested in the previous answers, i.e. the Finalizer thread can't process the finalizer queue fast enough.

如果在运行“慢动作”模式时,问题消失了,那么问题可能是在前面的答案中提出的问题之一,即终结器线程不能足够快地处理终结器队列。

If that is the problem, it sounds like you might need to do some non-trivial refactoring as described in the page Bringer128 linked to, e.g.

如果这是问题所在,听起来您可能需要做一些重要的重构,如在链接到的页面Bringer128中所描述的。

Now let's look at how to write classes that require postmortem cleanup so that their users do not encounter the problems previously outlined. The best way to do so is to split such classes into two -- one to hold the data that need postmortem cleanup, the other to hold everything else -- and define a finalizer only on the former

现在让我们看看如何编写需要进行死后清理的类,以便他们的用户不会遇到前面提到的问题。最好的方法是将此类类分为两类——一个用于保存需要事后清理的数据,另一个用于保存其他所有内容——并仅在前者上定义终结器

#2


9  

Both quotes say:

这两个报价说:

An exception will cause finalization of this object to be halted/terminated.

一个异常将导致这个对象的终结被停止/终止。

Both quotes also say:

两个报价也说:

The uncaught exception is ignored (i.e. not logged or handled by the VM in any way)

未被捕获的异常被忽略(例如,没有被记录或被VM以任何方式处理)

So that answers the first half of your question. I don't know enough about Finalizers to give you advice on tracking down your memory leak though.

这就回答了你问题的前半部分。我对Finalizers的了解还不够多,我想给你一些关于追踪内存泄漏的建议。

EDIT: I found this page which might be of use. It has advice such as setting fields to null manually in finalizers to allow the GC to reclaim them.

编辑:我找到了这个可能有用的页面。它提供了一些建议,比如在终结器中手动将字段设置为null,以允许GC回收它们。

EDIT2: Some more interesting links and quotes:

更多有趣的链接和引用:

From Anatomy of a Java Finalizer

从Java终结器的剖析

Finalizer threads are not given maximum priorities on systems. If a "Finalizer" thread cannot keep up with the rate at which higher priority threads cause finalizable objects to be queued, the finalizer queue will keep growing and cause the Java heap to fill up. Eventually the Java heap will get exhausted and a java.lang.OutOfMemoryError will be thrown.

终结器线程在系统上没有获得最大优先级。如果“终结器”线程无法跟上高优先级线程导致可终结对象排队的速度,那么终结器队列将继续增长,并导致Java堆填满。最终,Java堆将被耗尽,并使用Java .lang。就会抛出OutOfMemoryError。

and also

it's not guaranteed that any objects that have a finalize() method are garbage collected.

不能保证任何具有finalize()方法的对象都是垃圾收集的。

EDIT3: Upon reading more of the Anatomy link, it appears that throwing exceptions in the Finalizer thread really slows it down, almost as much as calling Thread.yield(). You appear to be right that the Finalizer thread will eventually flag the object as able to be GC'd even if an exception is thrown. However, since the slowdown is significant it is possible that in your case the Finalizer thread is not keeping up with the object-creation-and-falling-out-of-scope rate.

EDIT3:在阅读了更多的解剖学链接之后,似乎在终结器线程中抛出异常会降低它的速度,几乎与调用thread .yield()一样慢。即使抛出异常,终结器线程最终也将标记对象为GC,这似乎是正确的。但是,由于放缓是显著的,所以在您的示例中终结器线程可能没有跟上对象创建和超出范围的速度。

#3


3  

The item 7 of Effective Java second edition is: "Avoid finalizers". I strongly recommend you to read it. Here is an extract that may help you:

有效的Java第二版的第7项是:“避免终结器”。我强烈建议你读一读。这里有一个可以帮助你的摘录:

"Explicit termination methods are typically used in combination with try-finally construct to ensure termination"

“显式终止方法通常与try-finally结构结合使用,以确保终止”

#4


0  

I have same issue with you (below picture). For our case, it because an object has wait(0) in its finalize and it never get notified, which block the java.lang.ref.Finalizer$FinalizerThread. More reference

我和你有同样的问题(下图)。对于我们的例子来说,因为对象在其finalize中等待(0),而它永远不会得到通知,从而阻塞了java.lang.ref.Finalizer$FinalizerThread。更多的参考

  • The Secret Life Of The Finalizer
  • 终结者的秘密生活
  • java/lang/ref/Finalizer.java
  • java / lang / ref / Finalizer.java

objects retained by Finalizer

#5


0  

i have once see a similar problem, that is the finalizer thread can not catch up with the rate of generating finalizable objects.

我曾经遇到过一个类似的问题,那就是终结器线程无法赶上生成可终结对象的速度。

my solution is the make a closed loop control, by use the MemoryMXBean .getObjectPendingFinalizationCount(), a PD( proportinal and diffrential) control algo to control the speed we generate the finalizable objects, since we have a single entry to create it, just sleep number of seconds with the result of pd algo. it works well, though you need to tune the paramter for pd algo.

我的解决方案是使用MemoryMXBean . getobjectpendingfinalizationcount(),一个PD(proportinal和diffrential)控件algo,通过它来控制生成可终结对象的速度,因为我们只有一个条目来创建它,使用PD algo的结果只需要睡眠数秒。它工作得很好,尽管你需要为pd algo调整参数。

hope it helps.

希望它可以帮助。


推荐阅读
author-avatar
蓬从蓉Tahirah
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有