finalize()方法是Object类中提供的一个方法,在GC准备释放对象所占用的内存空间之前,它将首先调用finalize()方法。其在Object中定义如下:
protected void finalize() throws Throwable { }
finalize()是由FinalizerThread线程处理的,每一个即将被回收的并且包含有finalize()方法的对象都会在正式回收前加入FinalizerThread的执行队列,该队列为java.lang.ref.ReferenceQueue引用队列,内部实现为链表结构,队列中每一项为java.lang.ref.Finalizer引用对象,它本质也是一个引用。
实例:jvm参数设置为:
-Xmx10m -Xms10m -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath="C:\jvm\f.dump"
(1)-XX:+HeapDumpOnOutOfMemoryError参数表示当JVM发生OOM时,自动生成DUMP文件。
(2)-XX:HeapDumpPath=${目录}参数表示生成DUMP文件的路径,也可以指定文件名称,例如:-XX:HeapDumpPath=${目录}/java_heapdump.hprof。如果不指定文件名,默认为:java_<pid>_<date>_<time>_heapDump.hprof。
package com.jvm;
public class LongFinalize {
public static class LF{
private byte[] content=new byte[512];
@Override
protected void finalize(){
try{
System.out.println(Thread.currentThread().getId()+"="+Thread.currentThread().getName());
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void main(String[] args){
long b=System.currentTimeMillis();
for(int i=0;i<50000;i++){
LF f=new LF();
}
long e=System.currentTimeMillis();
System.out.println(e-b);
}
}
Myeclipse下部分为:
GC说明如下:
新生代ParallelGC,老年代ParallelOldGC回收器
新生代ParallelGC
0.880:[GC [PSYoungGen:16448k->xxxx]]
老年代ParallelOldGC回收器
1.500:[Full GC[PSYoungGen:2533k->xxxxxxxxxxxxxxxx]]
[GC (Allocation Failure) [PSYoungGen: 2048K->480K(2560K)] 2048K->1936K(9728K), 0.0019972 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
3=Finalizer
[GC (Allocation Failure) [PSYoungGen: 2528K->504K(2560K)] 3984K->3896K(9728K), 0.0104340 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[GC (Allocation Failure) [PSYoungGen: 2552K->504K(2560K)] 5944K->6000K(9728K), 0.0018918 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 504K->0K(2560K)] [ParOldGen: 5496K->5821K(7168K)] 6000K->5821K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0175480 secs] [Times: user=0.06 sys=0.02, real=0.02 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->991K(2560K)] [ParOldGen: 5821K->6842K(7168K)] 7869K->7833K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0076535 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2046K(2560K)] [ParOldGen: 6842K->6842K(7168K)] 8890K->8888K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0054653 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2047K(2560K)] [ParOldGen: 6842K->6842K(7168K)] 8890K->8889K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0069528 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2047K(2560K)] [ParOldGen: 6855K->6855K(7168K)] 8903K->8902K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0049591 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6855K->6855K(7168K)] 8902K->8902K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0044946 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2047K(2560K)] [ParOldGen: 6867K->6867K(7168K)] 8915K->8915K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0047354 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2047K(2560K)] [ParOldGen: 6880K->6880K(7168K)] 8928K->8928K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0057285 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 6880K->6880K(7168K)] 8928K->8928K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0042706 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2047K(2560K)] [ParOldGen: 6892K->6892K(7168K)] 8940K->8940K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0047342 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 6905K->6905K(7168K)] 8953K->8953K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0042674 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 6917K->6917K(7168K)] 8965K->8965K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0058332 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 6929K->6929K(7168K)] 8977K->8977K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0041256 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 6941K->6941K(7168K)] 8989K->8989K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0043058 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 6954K->6954K(7168K)] 9002K->9002K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0045423 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 6966K->6966K(7168K)] 9014K->9014K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0061106 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 6978K->6978K(7168K)] 9026K->9026K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0047990 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 6990K->6990K(7168K)] 9038K->9038K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0044154 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7002K->7002K(7168K)] 9050K->9050K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0055408 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7014K->7014K(7168K)] 9062K->9062K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0054080 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7027K->7027K(7168K)] 9075K->9075K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0045925 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7039K->7039K(7168K)] 9087K->9087K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0044476 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7051K->7051K(7168K)] 9099K->9099K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0052376 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7063K->7063K(7168K)] 9111K->9111K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0059534 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7075K->7075K(7168K)] 9123K->9123K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0048936 secs] [Times: user=0.11 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7087K->7087K(7168K)] 9135K->9135K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0040770 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7100K->7100K(7168K)] 9148K->9148K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0047803 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7112K->7112K(7168K)] 9160K->9160K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0056748 secs] [Times: user=0.09 sys=0.02, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7124K->7124K(7168K)] 9172K->9172K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0045878 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7136K->7136K(7168K)] 9184K->9184K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0042943 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7148K->7148K(7168K)] 9196K->9196K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0057041 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7160K->7160K(7168K)] 9208K->9208K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0051901 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7167K->7167K(7168K)] 9215K->9215K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0038905 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Allocation Failure) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7167K->7155K(7168K)] 9215K->9203K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0136251 secs] [Times: user=0.00 sys=0.01, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7161K->7161K(7168K)] 9209K->9209K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0042486 secs] [Times: user=0.00 sys=0.02, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7167K->7167K(7168K)] 9215K->9215K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0043621 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Allocation Failure) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7167K->7167K(7168K)] 9215K->9215K(9728K), [Metaspace: 2652K->2652K(1056768K)], 0.0035749 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
java.lang.OutOfMemoryError: Java heap space
Dumping heap to D:\jvm\f.dump ...
Unable to create D:\jvm\f.dump: File exists
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7167K->7166K(7168K)] 9215K->9214K(9728K), [Metaspace: 2653K->2653K(1056768K)], 0.0083556 secs] [Times: user=0.11 sys=0.00, real=0.01 secs]
Exception in thread "main" [Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7167K->7167K(7168K)] 9215K->9215K(9728K), [Metaspace: 2670K->2670K(1056768K)], 0.0046120 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7167K->7167K(7168K)] 9215K->9215K(9728K), [Metaspace: 2676K->2676K(1056768K)], 0.0041574 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7167K->7167K(7168K)] 9215K->9215K(9728K), [Metaspace: 2676K->2676K(1056768K)], 0.0041233 secs] [Times: user=0.11 sys=0.00, real=0.00 secs]
[Full GC (Allocation Failure) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7167K->7167K(7168K)] 9215K->9215K(9728K), [Metaspace: 2676K->2676K(1056768K)], 0.0055191 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 2048K->2048K(2560K)] [ParOldGen: 7167K->7165K(7168K)] 9215K->9213K(9728K), [Metaspace: 2676K->2676K(1056768K)], 0.0072774 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
Heap
PSYoungGen total 2560K, used 2048K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
eden space 2048K, 100% used [0x00000000ffd00000,0x00000000fff00000,0x00000000fff00000)
from space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
to space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
ParOldGen total 7168K, used 7166K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
object space 7168K, 99% used [0x00000000ff600000,0x00000000ffcffa98,0x00000000ffd00000)
Metaspace used 2682K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 285K, capacity 386K, committed 512K, reserved 1048576K
Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "main"
根据导出.dump文件,在myeclipse中使用mat工具打开。