Original title: I see the following section of code in the thinking in Java.
: Garbage.java
Demonstration of the garbage
Collector and Finalization
Class Chair {
Static Boolean gcrun=false;
Static Boolean f=false;
static int created=0;
static int finalized=0;
int i;
Chair () {
i=++created;
if (created==47)
System.out.println ("Created 47");
}
protected void Finalize () {
if (!gcrun) {
Gcrun=true;
System.out.println (
"Beginning to finalize" +created+
"Chairs have been created");
}
if (i==47) {
System.out.println (
"Finalizing Chair #47," +
"Setting flag to stop Chair creation");
F=true;
}
finalized++;
if (finalized>=created)
System.out.println (
"All" +finalized+ "finalized");
}
}
public class garbage{
public static void Main (string[] args) {
if (args.length==0) {
System.out.println ("usage:\n" +
"Java garbage before\n or:\n" +
"Java garbage after");
Return
}
while (! CHAIR.F) {
New Chair ();
New String ("to take up Spaces");
}
System.out.println (
"After all chairs have been created:\n" +
"Total created =" +chair.created+
", Total finalized =" +chair.finalized);
if (Args[0].equals ("before")) {
SYSTEM.OUT.PRINTLN ("GC ():");
System.GC ();
System.out.println ("Runfinalization ():");
System.runfinalization ();
}
System.out.println ("Bye");
if (Args[0].equals ("after"))
System.runfinalizersonexit (TRUE);
}
}///:-
Why do I perform Java gerbage before
Later, when all objects are created (such as 8,000), only 2000 are cleared (indeterminate)
You should only create 47 objects.
Analysis: First, the object is created in a loop and is created, not referenced, which means that the object is automatically disposed of by the system as garbage. Note, however, that the Finalize () method is not executed immediately, and that the execution time is entirely determined by the system. So it is quite possible that 20,000 objects have been created to begin the cleanup of one of the objects (this may be related to time or the usage of the system's content). Look at a piece of code in the Finalize () method:
if (!gcrun) {
Gcrun = true;
System.out.println (
"\nbeginning to finalize" + created + "chairs have been Created\nat");
}
The result would be this:
Beginning to finalize after 25038 chairs have been created
The creation of the object is still in progress (since the Finalize object is still 47, CHAIR.F or false). So chair.created will continue to increase.
Until 47 objects have been cleared, CHAIR.F is set to true, and the loop to create the object ends. Look at a piece of code in the Main method:
System.out.println (
"\nafter all chairs have been created:\n"
+ "Total created ="
+ chair.created
+ ", Total finalized ="
+ chair.finalized+ "\ n");
As mentioned above, the chair.created is increasing, and before this code executes, there will be N objects released, so the finalized also increases.
The result may be this:
Total created =29096,total finalized =73
In fact, this process has a lot to do with the JVM you use, and the results may be quite different. But one thing is to be sure, that is, we are not sure when the JVM will do the cleanup of objects (which is also what this code in Java thinking) can erase when the object is just "useless", or it may lag for a few seconds or even never erase.
If your development environment supports tracking, you can run it as a step, and you'll see that the results are very different. I'll show you the results of my operation:
Created 47
Beginning to finalize After25038chairs have been created
Finalizing Chair #47, Setting flag to stop Chair creation
After all chairs have been created:
Total created =45993,total finalized =1800
GC ():
Runfinalization ():
Bye
All45993finalized
Note the last line, because the JVM has enough time to clear all the objects because of a human pause.
Conclusion: The Finalize () method cannot be expected to work stably, especially if it is not relied upon to do the closing of the file, and because of the uncertainty of Finalize (), the results you want are not always available. In fact we just need to know all the useless objects, and the JVM will clear itself.
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