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 destruction of objects in Java

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what is the destruction of objects in Java". In the daily operation, I believe that many people have doubts about what is the destruction of objects in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what is the destruction of objects in Java"! Next, please follow the editor to study!

In daily development, we all know that Java's memory is cleaned through the garbage collector, so how does it clean up useless objects?

Automatic memory collection in Java language is called garbage collection (Garbage Collection) mechanism, or GC for short. Garbage collection mechanism means that JVM is used to release the memory occupied by objects that are no longer in use.

The Java object needs to be cleaned after use. Object cleanup frees up the memory occupied by the object. When creating an object, the user must allocate memory for the object using the new operator. After the object is cleared, the memory is automatically reclaimed by the system without additional processing by the user. This is also a feature of the Java language that makes it easier for programmers to manage memory.

Generally speaking, there are two main situations in which an object is treated as garbage collection.

1) the reference to the object exceeds its scope of action.

{Object o = new Object (); / / the scope of the object o, beyond which objects will be regarded as garbage}

2) the object is assigned to null

{Object o = new Object (); o = null; / / the object assigned to null will be regarded as garbage}

A finalize () method of type protected is also provided in the Object class of Java, so any Java class can override this method to release related resources occupied by the object.

So the question comes again, what the heck is finalize ()? since this method of the object will be called, it means that all classes will have this method (after all, all classes will be recycled), naturally we think of the root class Object of java. Go in and have a look?

Protected void finalize () throws Throwable {}

The last line is really found, is an empty method, since the protected means that the specific method can be left to the subclass to implement, we said before that the object will be recycled only when it is no longer pointed to by any reference. So is that really the case? Let's take a look at a chestnut.

Public class User {private int money;public int getMoney () {return money;} public void setMoney (int money) {this.money = money;} public void cool () {String str=new String ();} @ Overrideprotected void finalize () throws Throwable {/ / TODO Auto-generated method stubif (money > 0) {System.out.println ("error");} else {System.out.println ("suceess");} super.finalize ();}}

Here we rewrite the finalize () method, so that if a person is not used up before destruction, the print of that person fails, otherwise the person is successful. Here is our code in main ()

Public class Test {public static void main (String args []) {User u1=new User (200); new Object (); new User (100);}}

The result of the run is nothing! At the end of the run, not only the U1 that the reference points to, but also new User (100) that does not have any reference points; it is unexpectedly not recycled. What's going on?

Let's see how it is explained in Thinking In Java.

What is in java is not always garbage collected, which means that objects may not be collected. Generally speaking, as long as the program runs out of storage space, the garbage collector will not take the initiative to reclaim the memory. If the program ends and the garbage collector has not released the space you created, as the program exits, the resources will be returned to the operating system. That's why our finalize () above has never been called.

If we want to see the effect, we can do it in the following ways:

Public class Test {public static void main (String args []) {User u1=new User (200); new Object (); new User (100); System.gc ();}}

System.gc (); will force the system garbage collector to work, and error will appear in the running effect.

Indicates that the object created by new User; has been recycled.

Note: calling the System.gc () or Runtime.gc () method does not guarantee that the collection operation will be performed, it just increases the likelihood that the Java garbage collector will collect the garbage as soon as possible.

Knowledge supplement:

In the heap area of the Java virtual machine, each object may be in one of three states.

1) reachable state: when an object is created, it is always in a reachable state as long as there are reference variables in the program that reference it.

2) Resurrection state: when the program no longer has any reference variables to refer to the object, the object enters the resurrection state. In this state, the garbage collector is ready to free the memory it occupies, and before releasing it, it calls the finalize () methods of it and other objects in the resurrectable state, which have the potential to bring the object back to reachable state.

3) unreachable state: when the Java virtual machine executes all the finalize () methods of the resurrectable object, if none of these methods make the object reachable, the garbage collector will actually reclaim the memory it occupies.

At this point, the study on "what is the destruction of objects in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Wechat

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

12
Report