Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the impact of using finalize method on garbage collection

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about the impact of finalize on garbage collection. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Java provides a finalize method that can help us release resources, similar to the destructor in C++. But at present, the common understanding is not to use it, why? It is because of the impact on the garbage collection of java virtual machines.

First, why does it matter?

We all know that if an object does not have any references, the java virtual machine thinks that the object is useless and garbage collects it, but if the object contains the finalize function, the nature is different. Why is it different?

When the java virtual machine is doing garbage collection, as soon as it sees that the object class contains the finalize function, it will give this function to FinalizerThread for processing, and the objects containing the finalize will be added to the FinalizerThread execution queue and use a linked list to string these objects that contain finalize.

His influence is that as long as the finalize is not executed, then these objects will always exist in the heap area, but here are only four objects that contain finalize, the impact is not so great, what if there are 10, 000 or 100, 000? That makes a big difference.

The principle of finalize is actually very simple. Let's briefly sort it out here:

(1) during initialization, the object will determine whether finalize has been rewritten by judging the two field flags has_finalizer_flag and RegisterFinalizersAtInit.

(2) if finalize is overridden, the current object is registered with the ReferenceQueue queue of FinalizerThread. The registered object is called Finalizer. The method is to call the register_finalizer function. At this point, the java virtual machine sees that there is a reference to this object, so it does not garbage collect.

(3) the object starts to be called, and the FinalizerThread thread is responsible for getting the Finalizer object from the ReferenceQueue queue. Start executing the finalize method, which is in the heap until it is executed.

(4) after the object is executed, the Finalizer object is removed from the queue. As soon as the object is no longer referenced, the java virtual machine carries out garbage collection.

This is the whole process. However, what we mainly look at here is the impact of the finalize method on garbage collection. In fact, in the third step, when the object contains finalize and enters the queue but has not been called, it will occupy memory all the time.

We use a case to analyze a wave:

II. Case demonstration

We create a class

1public class TestFinalizer {

2 public static class Fdd {

3 / / assign 1m

4 private byte [] content = new byte [1024,1024]

5 @ Override

6 protected void finalize () {

7 System.out.println ("finalize executed")

8}

9}

10 public static void main (String [] args) {

11 for (int I = 0; I < 1000; iTunes +) {

12 Fdd fdd = new Fdd ()

13}

14}

15}

Now that the class is created, let's set the parameters.

Maximum heap memory

2-Xmx5m

Minimum heap memory

4-Xms5m

Bad heap memory overflow error print

6-XX:+HeapDumpOnOutOfMemoryError

Save heap-related information in the following path

8-XX:HeapDumpPath=F:/a.dump

In the main method, 1000 Fdd objects are created, and if the finalize method is not executed, garbage collection will occur because there is no call, and there will be no problem with how many we create. But if there is a finalize method, it is different.

1java.lang.OutOfMemoryError: Java heap space

2Dumping heap to F:/a.dump...

3finalize is executed

4finalize is executed

5finalize is executed

6finalize is executed

7finalize is executed

8finalize is executed

9finalize is executed

10Unable to create F:/a.dump: File exists

11Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

12 at com.fdd.chapter2.TestFinalizer$Fdd. (TestFinalizer.java:6)

13 at com.fdd.chapter2.TestFinalizer.main (TestFinalizer.java:14)

We see that each object executes finalize, which is in the heap for a period of time before execution, and will be cleaned after execution, so you can see that the finalize method has been executed no less than 5 times. But once the object exceeds the 5m we set, there will be a memory overflow. In a word, there is object accumulation. Now use the MAT tool to analyze it.

The Mat tool is a plug-in, or you can download one yourself. When the download is complete, just open the a.dump we just generated.

The following picture shows the results of the analysis:

A this piece of content is Finalizer, that is, our Fdd object, b contains more, a mess of remaining information. Of course, you can also check some other information. It's all on MAT tools. There are also some finalizer that are being executed and those that are going to be executed.

After reading the above, do you have any further understanding of the impact of using the finalize method on garbage collection? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report