Cleanup of Java

Source: Internet
Author: User

Creation of Java

Http://www.cnblogs.com/maying3010/p/5866128.html

Scope of Java
The scope is usually the closest one to the inside of a curly brace. Out of scope, the object continues to occupy memory space, but we cannot access the object
Garbage collection in Java
1. When an object is out of scope or NULL, Java's garbage collection mechanism considers the object to be recyclable (not necessarily recycled). 2. When the garbage collector is ready to release memory, Finalize () is called first, and the memory occupied by the object is actually reclaimed when the next garbage collection action occurs. 3. System.GC () Method: This method simply recommends that the garbage collector reclaim the memory, not necessarily the actual recovery of memory.

Test1:

 Public classTestClass {intFlage = 0; TestClass (intflage) {System.out.println ("New Object TestClass");  This. Flage =Flage; } @Overrideprotected voidFinalize () {System.out.println ("Run this method:finalize () and this flag is" +flage); }     Public Static voidMain (string[] args) {TestClass T=NewTestClass (2);    System.GC (); }}output:
New Object TestClass

Test2:

 Public classTestClass {intFlage = 0; TestClass (intflage) {System.out.println ("New Object TestClass");  This. Flage =Flage; } @Overrideprotected voidFinalize () {System.out.println ("Run this method:finalize () and this flag is" +flage); }     Public Static voidMain (string[] args) {TestClass T=NewTestClass (2); T=NULL;    System.GC (); }}output:
New Object TestClass
Run this method:finalize () and this flag is 2

TEST3:

 Public classTestClass {intFlage = 0; TestClass (intflage) {System.out.println ("New Object TestClass");  This. Flage =Flage; } @Overrideprotected voidFinalize () {System.out.println ("Run this method:finalize () and this flag is" +flage); }    Static voidTest () {TestClass T=NewTestClass (3); }     Public Static voidMain (string[] args) {TestClass T=NewTestClass (2);        Test ();    System.GC (); }}output:

New Object TestClass
New Object TestClass
Run this method:finalize () and this flag is 3

Conditions triggered by the garbage collection mechanism
The JVM has a
high frequency of secondary GC, but because this GC occupies a very short time, it has little effect on the system. More notable is the trigger condition of the main GC, because it has a noticeable effect on the system. Overall, there are two conditions that trigger the main Gc:① when the application is idle, that is, the GC is called when no application thread is running. Because the GC is in the lowest priority thread, the GC thread is not called when the application is busy, except for the following conditions. GC is called when the ②java heap is low on memory. When the application thread is running and a new object is created during the run, if there is not enough memory space, the JVM will forcibly invoke the GC thread to reclaim the memory for the new allocation. If the GC still fails to meet the memory allocation requirements after one time, the JVM will take another two GC for further attempts, and if it still fails to meet the requirements, the JVM will report an "out of memory" error and the Java application will stop. Since the main GC is determined by the JVM in terms of the system environment, and the system environment is constantly changing, the main GC runs with uncertainty and cannot predict when it will inevitably occur, but it can be determined that for a long-running application, its main GC is repeated.
Principle
Make
a flag here, wait until you see the JVM and then write
Reference

Http://www.blogjava.net/wilesun/archive/2007/10/08/150955.html

Cleanup of Java

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.