This post is covering one of the Java built-in concepts called Finalizer. This concept is actually both well-hidden and well-known, depending whether you have bothered to take a look at the java.lang.Object class thoroughly enough. Right in the java.lang.Object itself, there is a method called finalize(). The implementation of the method is empty, but both the power and dangers lie on the JVM internal behaviour based upon the presence of such method.

When JVM detects that class has a finalize() method, magic starts to happen. So, lets go forward and create a class with a non-trivial finalize() method so we can see how differently JVM is handling objects in this case. For this, lets start by constructing an example program:

Example of Finalizable class

01.import java.util.concurrent.atomic.AtomicInteger;
02. 
03.class Finalizable {
04.static AtomicInteger aliveCount = new AtomicInteger(0);
05. 
06.Finalizable() {
07.aliveCount.incrementAndGet();
08.}
09. 
10.@Override
11.protected void finalize() throws Throwable {
12.Finalizable.aliveCount.decrementAndGet();
13.}
14. 
15.public static void main(String args[]) {
16.for (int i = 0;; i++) {
17.Finalizable f = new Finalizable();
18.if ((i % 100_000) == 0) {
19.System.out.format("After creating %d objects, %d are still alive.%n"new Object[] {i, Finalizable.aliveCount.get() });
20.}
21.}
22.}
23.}

The example is creating new objects in an unterminated loop. These objects use static aliveCount variable to keep track how many instances have already been created. Whenever a new instance is created, the counter is incremented and whenever the finalize() is called after GC, the counter value is reduced.

So what would you expect from such a simple code snippet? As the newly created objects are not referenced from anywhere, they should be immediately eligible for GC. So you might expect the code to run forever with the output of the program to be something similar to the following:

1.After creating 345,000,000 objects, 0 are still alive.
2.After creating 345,100,000 objects, 0 are still alive.
3.After creating 345,200,000 objects, 0 are still alive.
4.After creating 345,300,000 objects, 0 are still alive.

Apparently this is not the case. The reality is completely different, for example in my Mac OS X on JDK 1.7.0_51, I see the program failing with java.lang.OutOfMemoryError: GC overhead limitt exceeded just about after ~1.2M objects have been created:

01.After creating 900,000 objects, 791,361 are still alive.
02.After creating 1,000,000 objects, 875,624 are still alive.
03.After creating 1,100,000 objects, 959,024 are still alive.
04.After creating 1,200,000 objects, 1,040,909 are still alive.
05.Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
06.at java.lang.ref.Finalizer.register(Finalizer.java:90)
07.at java.lang.Object.(Object.java:37)
08.at eu.plumbr.demo.Finalizable.(Finalizable.java:8)
09.at eu.plumbr.demo.Finalizable.main(Finalizable.java:19)

Garbage Colletion behaviour

To understand what is happening, we would need to take a look at our example code during the runtime. For this, lets run our example with -XX:+PrintGCDetails flag turned on:

01.[GC [PSYoungGen: 16896K->2544K(19456K)] 16896K->16832K(62976K), 0.0857640 secs] [Times: user=0.22 sys=0.02, real=0.09 secs]
02.[GC [PSYoungGen: 19440K->2560K(19456K)] 33728K->31392K(62976K), 0.0489700 secs] [Times: user=0.14 sys=0.01, real=0.05 secs]
03.[GC-- [PSYoungGen: 19456K->19456K(19456K)] 48288K->62976K(62976K), 0.0601190 secs] [Times: user=0.16 sys=0.01, real=0.06 secs]
04.[Full GC [PSYoungGen: 16896K->14845K(19456K)] [ParOldGen: 43182K->43363K(43520K)] 60078K->58209K(62976K) [PSPermGen: 2567K->2567K(21504K)], 0.4954480 secs] [Times: user=1.76 sys=0.01, real=0.50 secs]
05.[Full GC [PSYoungGen: 16896K->16820K(19456K)] [ParOldGen: 43361K->43361K(43520K)] 60257K->60181K(62976K) [PSPermGen: 2567K->2567K(21504K)], 0.1379550 secs] [Times: user=0.47 sys=0.01, real=0.14 secs]
06.--- cut for brevity---
07.[Full GC [PSYoungGen: 16896K->16893K(19456K)] [ParOldGen: 43351K->43351K(43520K)] 60247K->60244K(62976K) [PSPermGen: 2567K->2567K(21504K)], 0.1231240 secs] [Times: user=0.45 sys=0.00, real=0.13 secs]
08.[Full GCException in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
09.[PSYoungGen: 16896K->16866K(19456K)] [ParOldGen: 43351K->43351K(43520K)] 60247K->60218K(62976K) [PSPermGen: 2591K->2591K(21504K)], 0.1301790 secs] [Times: user=0.44 sys=0.00, real=0.13 secs]
10.at eu.plumbr.demo.Finalizable.main(Finalizable.java:19)

From the logs we see that after just a few minor GCs cleaning Eden, the JVM turns to a lot more expensive Full GC cycles cleaning tenured and old space. Why so? As nothing is referring our objects, shouldn’t all the instances die young in Eden? What is wrong with our code?

To understand, the reasons for GC behaving as it does, let us do just a minor change to the code and remove the body of the finalize() method. Now the JVM detects that our class does not need to be finalized and changes the behaviour back to “normal”. Looking at the GC logs we would see only cheap minor GCs running forever.


As in this modified example nothing indeed refers to the objects in Eden (where all objects are born), the GC can do a very efficient job and discard the whole Eden at once.  So immediately, we have cleansed the whole Eden, and the unterminated loop can continue forever.

In our original example on the other hand, the situation is different. Instead of objects without any references, JVM creates a personal watchdog for each and every one of the Finalizableinstances. This watchdog is an instance of Finalizer. And all those instances in turn are referenced by the Finalizer class. So due to this reference chain, the whole gang stays alive.

Now with the Eden full and all objects being referenced, GC has no other alternatives than to copy everything into Survivor space. Or worse, if the free space in Survivor is also limited, then expand to the Tenured space. As you might recall, GC in Tenured space is a completely different beast and is a lot more expensive than “lets throw away everything” approach used to clean Eden.

Finalizer queue

Only after the GC has finished, JVM understands that apart from the Finalizers nothing refers to our instances, so it can mark all Finalizers pointing to those instances to be ready for processing. So the GC internals add all Finalizer objects to a special queue atjava.lang.ref.Finalizer.ReferenceQueue.

Only when all this hassle is completed our application threads can proceed with the actual work. One of those threads is now particularly interesting for us – the “Finalizer” daemon thread. You can see this thread in action by taking a thread dump via jstack:

01.My Precious:~ demo$ jps
02.1703 Jps
03.1702 Finalizable
04.My Precious:~ demo$ jstack 1702
05. 
06.--- cut for brevity ---
07."Finalizer" daemon prio=5 tid=0x00007fe33b029000 nid=0x3103 runnable [0x0000000111fd4000]
08.java.lang.Thread.State: RUNNABLE
09.at java.lang.ref.Finalizer.invokeFinalizeMethod(Native Method)
10.at java.lang.ref.Finalizer.runFinalizer(Finalizer.java:101)
11.at java.lang.ref.Finalizer.access$100(Finalizer.java:32)
12.at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:190)
13.--- cut for brevity ---

From the above we see the “Finalizer” daemon thread running. “Finalizer” thread is a  thread with just a single responsibility. The thread runs an unterminated loop blocked waiting for new instances to appear in java.lang.ref.Finalizer.ReferenceQueue queue. Whenever the “Finalizer” threads detects new objects in the queue, it pops the object, calls the finalize() method and removes the reference from Finalizer class, so the next time the GC runs the Finalizer and the referenced object can now be GCd.

So we have two unterminated loops now running in two different threads. Our main thread is busy creating new objects. Those objects all have their personal watchdogs called Finalizer which are being added to the java.lang.ref.Finalizer.ReferenceQueue by the GC. And the “Finalizer” thread is processing this queue, popping all the instances from this queue and calling the finalize() methods on the instances.

Most of the time you would get away with this. Calling the finalize() method should complete faster than we actually create new instances.  So in many cases, the “Finalizer” thread would be able to catch up and empty the queue before the next GC pours more Finalizers into it. In our case, it is apparently not happening.

Why so? The “Finalizer” thread is run at a lower priority than the main thread. In means that it gets less CPU time and is thus not able to catch up with the pace objects are being created. And here we have it – the objects are created faster than the “Finalizer” thread is able to finalize() them, causing all the available heap to be consumed. Result – different flavours of our dear friendjava.lang.OutOfMemoryError.

If you still do not believe me, take a heap dump and take a look inside. For example, when our code snipped is launched with -XX:+HeapDumpOnOutOfMemoryError parameter, I see a following picture in Eclipse MAT Dominator Tree:

As seen from the screenshot, my 64m heap is completely filled with Finalizers.

Conclusions

So to recap, the lifecycle of Finalizable objects is completely different from the standard behaviour, namely:

  • The JVM will create the instance of Finalizable object
  • The JVM will create an instance of the java.lang.ref.Finalizer, pointing to our newly created object instance.
  • java.lang.ref.Finalizer class holds on to the java.lang.ref.Finalizer instance that was just created. This blocks next minor GC from collecting our objects and is keeping them alive.
  • Minor GC is not able to clean the Eden and expands to Survivor and/or Tenured spaces.
  • GC detects that the objects are eligible for finalizing and adds those objects to thejava.lang.ref.Finalizer.ReferenceQueue
  • The queue will be processed by “Finalizer” thread, popping the objects one-by-one and calling their finalize() methods.
  • After finalize() is called, the “Finalizer” thread removes the reference from Finalizer class, so during the next GC the objects are eligible to be GCd.
  • The “Finalizer” thread competes with our “main” thread, but due to lower priority gets less CPU time and is thus never able to catch up.
  • The program exhausts all available resources and throws OutOfMemoryError.

Moral of the story? Next time, when you consider finalize() to be superior to the usual cleanup, teardown or finally blocks, think again. You might be happy with the clean code you produced, but the ever-growing queue of Finalizable objects thrashing your tenured and old generations might indicate the need to reconsider.

reference from :http://java.dzone.com/articles/debugging-understand-finalizer

Debugging to Understand Finalizer--reference的更多相关文章

  1. debugging tools

    https://blogs.msdn.microsoft.com/debugdiag/ https://blogs.msdn.microsoft.com/debuggingtoolbox/2012/1 ...

  2. 基于dalvik模式下的Xposed Hook开发的某加固脱壳工具

    本文博客地址:http://blog.csdn.net/qq1084283172/article/details/77966109 这段时间好好的学习了一下Android加固相关的知识和流程也大致把A ...

  3. Cheatsheet: 2014 06.01 ~ 06.30

    Mobile Developing iOS8 Apps Using Swift – Part 1- Hello World The Insider's Guide to Android Intervi ...

  4. 【转】java线上程序排错经验2 - 线程堆栈分析

    前言 在线上的程序中,我们可能经常会碰到程序卡死或者执行很慢的情况,这时候我们希望知道是代码哪里的问题,我们或许迫切希望得到代码运行到哪里了,是哪一步很慢,是否是进入了死循环,或者是否哪一段代码有问题 ...

  5. 一些方便系统诊断的bash函数

    原文地址:一些方便系统诊断的bash函数 一些方便系统诊断的bash函数:http://hongjiang.info/common-bash-functions/ 这段脚本包含100多个bash函数, ...

  6. FinalizableReference, FinalizablePhantomReference, FinalizableReferenceQueue

    FinalizableReference /* * Copyright (C) 2007 The Guava Authors * * Licensed under the Apache License ...

  7. 通过Windows Visual Studio远程调试WSL2中的.NET Core Linux应用程序

    最近两天在Linux中调试.NET Core应用程序,同时我发现在Linux中调试.NET Core应用程序并不容易.一直习惯在Visual Studio中进行编码和调试.现在我想的是可以简单快速的测 ...

  8. Hibernate Validator 6.0.9.Final - JSR 380 Reference Implementation: Reference Guide

    Preface Validating data is a common task that occurs throughout all application layers, from the pre ...

  9. Sphinx 2.2.11-release reference manual

    1. Introduction 1.1. About 1.2. Sphinx features 1.3. Where to get Sphinx 1.4. License 1.5. Credits 1 ...

随机推荐

  1. Spring事务管理--多个ORM框架在使用时的情况分析

    公司的项目已经接近尾声了,总结一下项目中用到的技术,我发现项目中的有些东西还是挺模糊的,只是知道这么用就行了.并不清楚其中的原理.由于公司的项目比较老,是7年前的一个项目了,中间一直有人在维护,也是在 ...

  2. 近期刷题的c语言总结。

    首先是三个数学函数... /* 函数名: floor 功 能: 下舍入,返回小于或者等于指定表达式的最大整数 说明:返回x的下限,如74.12的下限为74,-74.12的下限为-75.返回值为floa ...

  3. Android自定义组合控件

    今天和大家分享下组合控件的使用.很多时候android自定义控件并不能满足需求,如何做呢?很多方法,可以自己绘制一个,可以通过继承基础控件来重写某些环节,当然也可以将控件组合成一个新控件,这也是最方便 ...

  4. 将win7电脑变身WiFi热点

    转自:http://bbs.feng.com/read-htm-tid-2167498.html 开启windows 7的隐藏功能:虚拟WiFi和SoftAP(即虚拟无线AP),就可以让电脑变成无线路 ...

  5. 免费的SqlServer优化辅助工具:SqlOptimize (原创)

    主要用于收集客户服务器的数据库运行情况,导出-导入到本地分析. 本工具不会修改你的数据和结构,只会读取相关数据. 1)工具软件下载 http://files.cnblogs.com/files/dud ...

  6. html总结----------------------看这个就够了

    HTML是我们学习Javaweb的第一步 很好地掌握门课是非常有必要的!下面就是我在听资深老师讲课的笔记!个人觉得非常不错!希望可以帮助到那些在学习javaweb路上的 朋友们!从今天 陆续的整理这门 ...

  7. 【原】Comparator和Comparable的联系与区别

    1.知识点了解 Comparator和Comparable都是用用来实现集合中元素的比较.排序的,所以,经常在集合外定义Comparator接口的方法和集合内实现Comparable接口的方法中实现排 ...

  8. Windows下部署Git Server端

    解决方案一: (注意:如果英文阅读没太大障碍,直接到Bonobo官网看相关文档即可.) win7(windows server 2008应该也可以,没测试过)+ IIS7.5(win7自带的)+ Gi ...

  9. 2015美团网 哈工大 第k个排列

    leetcode 上的Permutation Sequence 下面是可执行代码 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 以1 开头 123,132,共2!个数 2 开 ...

  10. editpuls查找替换通配符

    1  \t    Tab character.         tab符号 2  \n    New line.              新的一行(换行符) 3  .     Matches any ...