This article brings you relevant knowledge about java. It mainly introduces the use of object destruction and finalize methods in Java. It has a good reference value. Let’s take a look at it together. I hope Helpful to everyone.
Recommended study: "java video tutorial"
Destruction method in C Used to release resources and destroy the object itself.
In Java, due to the existence of GC, we do not need to manually reclaim memory, which greatly reduces the workload and improves the security of the program. But Java does have a function similar to the destructor in C.
Overload this method to perform some operations when the class is recycled by GC.
The following is an example of a class implementing finalize.
The Aoo class has an int and a String property, overloads toString and prints the object and its creation time in the construction, and prints the object and the calling time in finalize.
Aoo Class
public class Aoo { private int id; private String name; public Aoo(){ this(0, null); } public Aoo(int id, String name){ this.id = id; this.name = name; System.out.println(this.toString() + " now create:" + System.currentTimeMillis()); } /* * 省略get/set/toString */ protected void finalize() throws Throwable{ super.finalize(); System.out.println(this.toString() + "now finalize:" + System.currentTimeMillis()); } }
First, a simple test
main method
public class FinalizeTest { public static void main(String[] args) throws Exception { Aoo a = new Aoo(1, "a"); a = null; System.gc() Thread.sleep(2000); System.exit(0); } }
Print the result :
id:1 name:a now create:1497547723036
id:1 name:anow finalize:1497547724059
The GC is manually called here to clean up the memory, and if you comment out System.gc();, the printed result is like this:
id:1 name:a now create:1497547846923
In other words, without specifically calling the GC, the finalize method is not called at all, which means that the object is not actively recycled at all.
Contrary to imagination, GC operates in a lazy manner. That is to say, when there is no place in the memory, GC will not actively recycle objects. In order to verify this idea, I created a Threads are used to continuously consume memory and do not actively call GC.
ThreadA class
public class ThreadA implements Runnable{ public void run() { List<Integer> list = new ArrayList<Integer>(); int i = 0; while(true){ list.add(i); i++; } } }
main method
public class FinalizeTest { public static void main(String[] args) throws Exception { Aoo a = new Aoo(1, "a"); a = null; ThreadA ta = new ThreadA(); Thread t = new Thread(ta); t.start(); Thread.sleep(2000); System.exit(0); } }
Print result:
id: 1 name:a now create:1497548135268
id:1 name:anow finalize:1497548135386
Although GC was not called manually this time, the finalize method still ran, that is to say, only if The GC will only run when the memory is consumed and the GC needs to clean up the memory.
Such a finalize method is indeed unreliable. It is not even sure whether it can be called, let alone complete any specific operation. If you need to close the stream and recover resources, it is better to call a method manually, or in Resources are released uniformly in the final block.
Try to re-reference in the finalize method to prevent GC from recycling
The modified Aoo is as follows
public class Aoo { public static Aoo SAVE = null; private int id; private String name; public Aoo(){ this(0, null); } public Aoo(int id, String name){ this.id = id; this.name = name; System.out.println(this.toString() + " now create:" + System.currentTimeMillis()); } /* * 省略get/set/toString */ protected void finalize() throws Throwable{ super.finalize(); System.out.println(this.toString() + "now finalize:" + System.currentTimeMillis()); SAVE = this; } }
main method
public class FinalizeTest { public static void main(String[] args) throws Exception { Aoo.SAVE = new Aoo(1, "a"); Aoo.SAVE = null; System.gc(); Thread.sleep(500); System.out.println(Aoo.SAVE == null? "a is dead" : "a is alive" ); System.exit(0); } }
Print result:
id:1 name:a now create:1497551409195
id:1 name:anow finalize:1497551409201
a is alive
It can be seen here that the Aoo.SAVE object is indeed "resurrected", but this operation is limited. If the same trick is repeated, the object will not be "resurrected" again.
main method
public class FinalizeTest { public static void main(String[] args) throws Exception { Aoo.SAVE = new Aoo(1, "a"); Aoo.SAVE = null; System.gc(); Thread.sleep(500); System.out.println(Aoo.SAVE == null? "a is dead" : "a is alive" ); Aoo.SAVE = null; System.gc(); Thread.sleep(500); System.out.println(Aoo.SAVE == null? "a is dead" : "a is alive" ); System.exit(0); } }
Print result:
id:1 name:a now create:1497551587715
id:1 name :anow finalize:1497551587721
a is alive
a is dead
Note here that the two operations are the same, and the finalize method will only be called once by the system.
Aoo class
public class Aoo { private int id; private String name; public Aoo(){ this(0, null); } public Aoo(int id, String name){ this.id = id; this.name = name; System.out.println(this.toString() + " now create:" + System.currentTimeMillis()); } /* * 省略get/set/toString */ protected void finalize() throws Throwable{ super.finalize(); while(true){ System.out.println(this.toString() + "now finalize:" + System.currentTimeMillis()); Thread.sleep(100); } } }
main method
public class FinalizeTest { public static void main(String[] args) throws Exception { Aoo a1 = new Aoo(1 , "a1"); Aoo a2 = new Aoo(2 , "a2"); a1 = null; a2 = null; ThreadA ta = new ThreadA(); Thread t = new Thread(ta); t.start(); Thread.sleep(5000); System.exit(0); } }
Print result:
id: 1 name:a1 now create:1497552024252
id:2 name:a2 now create:1497552024252
id:1 name:a1now finalize:1497552024373
id:1 name:a1now finalize:1497552024503
id: 1 name:a1now finalize:1497552026848
id:1 name:a1now finalize:1497552028960
id:1 name:a1now finalize:1497552032363
The result is random and sometimes executed The finalize of a1 is sometimes executed by a2.
This result illustrates two points:
1. The thread priority of the finalze method is very low, and the time interval is quite uncertain and significantly greater than 100 milliseconds.
2. This infinite loop causes the finalize method of other objects to fail to execute.
We create a large number of Aoo objects and wait for the GC to reclaim the memory by itself.
In order to visually observe the calling of the finalize method, the printing code when the Aoo object is initialized is deleted.
main method
public class FinalizeTest { public static void main(String[] args) throws Exception { int i = 1; while(true){ Aoo a = new Aoo(i , "a" + i); i++; } } }
Let the program execute for about two minutes, then terminate it manually and check the output
1497554225913
id:269614 name:a269614now finalize:1497554226151
id:269614 name:a269614now finalize:1497554227635
id:269614 name:a269614now finalize:1497554227735
id:269614 name:a269614now finalize:1497554227836
id:269614 name:a269614now finalize:1497554229586
id:269614 name:a269614now finalize:1497554229686
id:269614 name:a269614now finalize:1497554229951
id:269614 name:a269614now finalize:1497554230051
id:269614 name:a269614now finalize:1497554230152
id:269614 name:a269614now finalize:1497554233699
id:269614 name:a269614now finalize:1497554233800
id:269614 name:a269614now finalize:1497554233900
id:269614 name:a269614now finalize:1497554234308
id:269614 name:a269614now finalize:1497554234408
id:269614 name:a269614now finalize:1497554234508
id:269614 name:a269614now finalize:1497554235053
id:269614 name:a269614now finalize:1497554235153
id:269614 name:a269614now finalize:1497554235253
id:269614 name:a269614now finalize:1497554235823
id:269614 name:a269614now finalize:1497554235923
id:269614 name:a269614now finalize:1497554236023
id:269614 name:a269614now finalize:1497554240324
id:269614 name:a269614now finalize:1497554240424
id:269614 name:a269614now finalize:1497554240525
id:269614 name:a269614now finalize:1497554241146
id:269614 name:a269614now finalize:1497554241247
id:269614 name:a269614now finalize:1497554241347
id:269614 name:a269614now finalize:1497554241448
id:269614 name:a269614now finalize:1497554242020
id:269614 name:a269614now finalize:1497554242120
id:269614 name:a269614now finalize:1497554242220
id:269614 name:a269614now finalize:1497554242321
id:269614 name:a269614now finalize:1497554242421
id:269614 name:a269614now finalize:1497554242521
id:269614 name:a269614now finalize:1497554248367
id:269614 name:a269614now finalize:1497554248467
id:269614 name:a269614now finalize:1497554248567
id:269614 name:a269614now finalize:1497554248667
id:269614 name:a269614now finalize:1497554249534
id:269614 name:a269614now finalize:1497554249634
id:269614 name:a269614now finalize:1497554249734
id:269614 name:a269614now finalize:1497554249835
id:269614 name:a269614now finalize:1497554255954
id:269614 name:a269614now finalize:1497554256055
id:269614 name:a269614now finalize:1497554256155
id:269614 name:a269614now finalize:1497554256255
id:269614 name:a269614now finalize:1497554256356
id:269614 name:a269614now finalize:1497554257285
id:269614 name:a269614now finalize:1497554257386
id:269614 name:a269614now finalize:1497554257486
id:269614 name:a269614now finalize:1497554257586
id:269614 name:a269614now finalize:1497554257686
id:269614 name:a269614now finalize:1497554268652
id:269614 name:a269614now finalize:1497554268753
id:269614 name:a269614now finalize:1497554268853
id:269614 name:a269614now finalize:1497554268953
id:269614 name:a269614now finalize:1497554269054
id:269614 name:a269614now finalize:1497554269154
id:269614 name:a269614now finalize:1497554277474
id:269614 name:a269614now finalize:1497554292852
id:269614 name:a269614now finalize:1497554301062
可以发现两个情况:
1.只有一个对象的finalize方法被执行了,也就是说这个死循环的finalize方法阻止了其他对象执行finalize方法
2.程序执行很快的一段时间后,finalize方法就开始执行,但是随着内存消耗的不断增加,finalize方法被执行的次数也就越来越少。至于为什么这样,我不知道= =
至此,我尝试了finalize方法的一些用法和特殊情况。可以看出,GC调用finalize方法存在巨大的不确定性,确实很不靠谱,不过通过这个方法,了解了一些关于GC的知识,也让我明白,虽然Java语言虽然具有高度的一致性等特点使之很容易上手,但是要做到对Java的精通,路还很远呢~~
推荐学习:《java视频教程》
The above is the detailed content of Java object destruction and use of finalize method. For more information, please follow other related articles on the PHP Chinese website!