Java classroom: the order in which garbage collection clears objects

Source: Internet
Author: User

----------------------------------------------------------------
 
(Author: AI Qun technology Xiao songyu)

One difference between Java and C is that when we write a C program, if we create a new
Object. At the same time, we must delete the object to clear it. The core of C language does not determine
Whether the object is no longer in use. During program execution, you do not need to remove or delete it all.
User-controlled. In fact, this original intention is very good, but it is written to large projects, or Multi
-In thread programs, these objects are often difficult to control. Sometimes we even try
Use a non-existing indicator.

In Java, JVM helps us manage this layer. Therefore, when an object is not in use, JV
M will remove it from the memory at & quot; appropriate & quot; time points.

All objects inherit objects. Therefore, an object is useful.
The method of is called finalize (). This method is defined when garbage C
The ollection calls finalize () of the object before removing it (). That's it.
Yes. When we implement our class, we 'd better overr this method.
IDE. In this way, when the garbage collection needs to clear objects, it will execute
To our own finalize ().

Therefore, we designed the bottom object. This object needs to be brought
Int value, which is an int value. When finalize () is executed
Print the int value so that we can see which garbage collection is going to clear
Object.

Public class testobject
{
Private int Itell;
Public testobject ()
{
This. Itell = 0;
}
Public testobject (int I)
{
This. Itell = I;
}
Public void finalize ()
{
System. Out. println (& quot; testobject id = & quot; + Itell );
}
}

Next, I wrote the following testfinalize. Java, this testfinalize
. Java will declare an array, which stores testobject objects.
I will set this array to null, indicating that I will no longer use this array.

Public class testfinalize
{
Public static void main (string argv [])
{
Testobject [] Testo = new testobject [2];
Testo [0] = new testobject (1 );
Testo [1] = new testobject (2 );
Testo = NULL;
System. Out. println (& quot; finish... & quot ;);
}
}

The execution result is

C:/temp/javajava testfinalize
Finish...

C:/temp/Java

Strange, is the garbage collection not performing finalize ?? No,
The garbage collection is not started at all. As mentioned earlier, the garbage collec
Tion will be started at the right time, and this testfinalize program is very small, JVM recognizes
So the program ends and the memory is released.

Isn't this the case ?? Not me
We can also use two methods to recommend JVM to execute the garbage collection, one
Is system. GC (); another recommended method is runtime. getruntime (). GC ()
, System. GC () will also execute runtime. getruntime (). GC (), no matter which one,
They all recommend that JVM execute another thread, which will scan
There are no used objects, they are cleared, and the memory they occupy is obtained. Therefore
Add testfinalize. Java as follows:

Public class testfinalize
{
Public static void main (string argv [])
{
Testobject [] Testo = new testobject [2];
Testo [0] = new testobject (1 );
Testo [1] = new testobject (2 );
Testo = NULL;
System. GC ();
System. Out. println (& quot; finish... & quot ;);
}
}

The execution result after compile is as follows:
C:/temp/javajava testfinalize
Testobject id = 1
Testobject id = 2
Finish...

C:/temp/Java

Yes, we see testobject id = 1 and testobject id = 2.
OK.

Is the problem solved in this way ?? None! He clears the array Testo [0] beforehand and then CLEARS T
Is there another sequence in esto [1 ??

First, let's take a look at the description of the garbage collection sequence in JLS.

12.6.2 finalizer invocations are not ordered

The Java programming language imposes no ordering on finalize
Method CILS. finalizers may be called in any order, or even con
Currently. As an example, if a circularly linked group of unfina
Lized objects becomes unreachable (or finalizer-reachable), then
All the objects may become finalizable together. Eventually, th
E finalizers for these objects may be invoked, in any order, or
Even concurrently using multiple threads. If the automatic Stora
GE manager later finds that the objects are unreachable, then th
EIR storage can be reclaimed.

It is straightforward to implement a class that will cause a s
ET of finalizer-like methods to be invoked in a specified order
For a set of objects when all the objects become unreachable. de
Fining such a class is left as an exercise for the reader.

According to this specification, we found that the JVM did not clear things in a certain order.
But actually ??

Let's modify testfinalize. Java to make it the underlying mode.

Public class testfinalize
{
Public static void main (string argv [])
{
Testobject [] Testo = new testobject [2];
Testo [1] = new testobject (2 );
Testo [0] = new testobject (1 );
Testo = NULL;
System. GC ();
System. Out. println (& quot; finish... & quot ;);
}
}

That is, this time, we first generate new testobject (2) and put it in Testo [
1] then generate new testobject (1), and put it in Testo [0].

The execution result is as follows:
C:/temp/javajava testfinalize
Testobject id = 2
Testobject id = 1
Finish...

C:/temp/Java

Yes, we actually run this program to produce the first object, garbage col
Lection first removes it.

When should I use finalize ()? Generally, you only need to remember one principle, that is
If we use resources such as JDBC connections, such
A socket to another machine, or even a TCP/IP server
In finalize (), we 'd better try to release these resources.


Execution Environment
1. Sun ultra 10
JDK 1.3.0 hotspot Client
2. Windows 2000
JDK 1.3.0 _ 01 hotspot Client
 
----------------------------------------------------------------

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.