Troubleshooting a java memory leak: finalization?

前端 未结 5 2220
难免孤独
难免孤独 2020-12-31 05:27

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 sus

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-31 05:43

    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)

    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.

    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.

    EDIT2: Some more interesting links and quotes:

    From Anatomy of a Java Finalizer

    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.

    and also

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

    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.

提交回复
热议问题