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

How to analyze the problem of Java garbage collection

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this issue, the editor will bring you about how to analyze the problems about Java garbage collection. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

The Java garbage collector only knows how to free memory allocated via new, so it doesn't know how to free this "special" memory of the object. To deal with this situation, Java allows a method called finalize () to be defined in the class. It works "assuming" that once the Java garbage collector is ready to free up the storage space occupied by the object, it will first call its finalize () method. And the memory occupied by the object will be reclaimed only when the next garbage collection occurs. So if you plan to use finalize (), you can do some important cleanup work during garbage collection. You may find that as long as the program is not on the verge of running out of storage space, the space occupied by the object will never be freed. If the program execution ends and the garbage collector does not release the storage space of any objects you create, as the program exits, all those resources will be returned to the operating system.

This strategy is appropriate because garbage collection has its own overhead, and if you don't use it, you don't have to pay for it. So you have no way to know if and when the garbage collector will execute. The objects you want to recycle are not necessarily recycled. What kind of objects are the finalize () methods used to clean up? If I want to clean up an object that contains other objects, should finalize () explicitly release those objects?

No-no matter how the object is created, the Java garbage collector is responsible for freeing all memory occupied by the object. This limits the need for finalize () to a special case, that is, allocating storage space for objects other than creating objects. However, as you can see, everything in Java is an object, so what is this special situation? It'seems that the reason for finalize () is that memory may be allocated in a way similar to that in C language. Not the usual practice in Java.

This mainly happens when using native methods, which are a way to call non-Java code in Java. Local methods currently only support C and custom codes, but they can call code written in other languages, so they can actually call any code. In non-Java code, C's malloc () function series may be called to allocate storage space, and unless the free () function is called, the storage space will not be freed, resulting in a memory leak. Of course, free () is a function in C and C++, so call it locally in finalize (). At this point, you may understand the reason not to use finalize () too much. System.gc (), force the garbage collector.

When is finalize () called? There are three situations. Object is called automatically when it is Garbage Collection, such as when running System.gc (). 2. The finalize method is called once for each object when the program exits. 3. Explicitly call the finalize method

In addition, normally, when an object is collected by the system as useless information, finalize () will be called automatically, but jvm does not guarantee that finalize () will be called, that is, the call to finalize () is uncertain, which is why sun does not advocate the use of finalize ().

Test code package test

/ * Test the garbage collector and the finalize () method * @ author Administrator * / public class GcTest {public static void main (String [] args) {Book b1 = new Book (); b1.setName ("new"); Book b2 = new Book (); b2.setName ("old") / * * point the b2 reference to null. Let the Book object referred to by the b 2 reference no longer have a reference to it. * when the garbage collector is running, let the object be recycled. * / b2 = null; / * * forcibly run the garbage collector. * / System.gc ();}} class Book {private String name; public String getName () {return name;} public void setName (String name) {this.name = name } @ Override protected void finalize () throws Throwable {/ / TODO Auto-generated method stub super.finalize (); System.out.println (getName () + "--> execute GC work.);}} package test / * Test the garbage collector and the finalize () method * @ author Administrator * / public class GcTest {public static void main (String [] args) {Book b1 = new Book (); b1.setName ("new"); Book b2 = new Book (); b2.setName ("old"); / * point the b2 reference to null. Let the Book object referred to by the b 2 reference no longer have a reference to it. * when the garbage collector is running, let the object be recycled. * / b2 = null; / * * forcibly run the garbage collector. * / System.gc ();} class Book {private String name; public String getName () {return name;} public void setName (String name) {this.name = name;} @ Override protected void finalize () throws Throwable {/ / TODO Auto-generated method stub super.finalize (); System.out.println (getName () + "--> perform GC work.);}}

Result code

Old--- > performs GC work.

The above is the editor for you to share how to analyze the problem of Java garbage collection, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Development

  • What is the HTML comment tag?

    This article mainly introduces the relevant knowledge of "what is the HTML comment tag". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "what is the HTML comment tag" can help you solve the problem. HTML comment:

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

    12
    Report