If you can tell the finalize usage well, the interviewer will think you are very senior

    When I was interviewing Java candidates, I sometimes asked the candidates about their skills in JVM through finalize. The general question is: Do you know the finalize method? Have you ever rewritten this method in your project? This knowledge point will be discussed in detail in this article.

    finalize() is a protected type method in the Object class. Subclasses (all classes are subclasses of Object) can override this method to achieve resource cleanup before recycling. The process related to this method is as follows.

    1 Once the Java virtual machine determines that an object is in a recyclable state through the "root search algorithm" just mentioned, it will determine whether the object has overridden the finalize method of the Object class, and if not, it will be recycled directly.

    2 If the finalize method has been overridden and the method has not been executed, the object will be put into the F-Queue queue, and another thread will periodically traverse the F-Queue queue and execute the finalize method of each object in the queue.

    3 After the finalize method is executed, the GC will again determine whether the object can be recycled, and if so, it will be recycled. If there is a strong reference to the object at this time, the object will be "resurrected", that is, in the "unrecyclable state".

    Through the following FinalizeDemo.java, we will demonstrate how to resurrect objects through the finalize method.

1	public class FinalizeDemo {
2		static FinalizeDemo obj = null;  
3	    //重写Object里的finalize方法  
4	    protected void finalize() throws Throwable {  
5	        System.out.println("In finalize()");  
6	        obj = this; //给obj加个强引用  
7	    }  	
8	    public static void main(String[] args) throws InterruptedException {  
9	        obj = new FinalizeDemo();  
10	        obj = null; //去掉强引用  
11	        System.gc(); //垃圾回收
12	        //sleep 1秒,以便垃圾回收线程清理obj对象 
13	        Thread.sleep(1000);
14	        if (null != obj) { //在finalize方法复活  
15	            System.out.println("Still alive.");  
16	        } else {  
17	            System.out.println("Not alive.");  
18	        } 
19	    }  
20	}

    In the 9th line of the main function, we allocate a memory space to the obj object defined in the second line, and remove the strong reference to the space pointed to by obj in the 10th line. In the 11th line, we start it through the System.gc method. A garbage collection mechanism.

    At this time, since there is no strong reference on the object pointed to by obj, this object can be recycled. Before recycling, the finalize method will be executed.

    In the finalize method rewritten in line 4, we add a strong reference to the obj object. In this case, after the finalize method is executed, the obj object does not meet the condition of being recycled, so the if... In the else judgment, follow the process on line 15 and output the sentence "still alive."

    However, because garbage collection and traversing the F-Queue queue are not the same thread, once this method is rewritten, it may cause the object to be delayed. If this method is put into the wrong code, it is very likely to cause the The object cannot be recycled.

    So back to the two questions at the beginning of this article.

    First, what does the finalize method do?

    In it, you can write the actions when the object is recycled, and you can talk about the specific process according to the meaning given in this article.

    Second, have you ever rewritten this method?

    Due to improper rewriting of finalize, the object cannot be recycled, so in the project, we generally do not rewrite this method, but will use the empty finalize method that comes with the Object class.

    Please pay attention to my official account: make progress together and make money together. In this official account, there will be many wonderful interview articles.

Guess you like

Origin blog.csdn.net/sxeric/article/details/112758593