Classes in JAVA

Source: Internet
Author: User

One: The life cycle of a class

  The life cycle of a class starts from the class being loaded, connected, and initialized!
To the end of the class uninstall!
01. In the life cycle of a class, the 2-based data of a class is located in the method area;
02. In the heap there will be a class object describing this category;

2.1 Loading: requires class loader
Load the contents of the class bytecode file into memory and convert the static data into
Run-time data structures in the method area! Generate a Class object in the heap;
This class object is the access entry for the method area class data!

2.2 Links:
01. Validation of classes during compilation
Legal???
001. Structure checking of class files
002. Semantic checks such as final modified variables found to be re-assigned during compilation
003. Byte code verification to ensure that the bytecode is recognized by the JVM

02. Preparation of Classes
Opens up space for all static decorated content in the class, and gives the initial value!!!
static int num = 100;
At this time num=0;

03. Parsing of Classes
Convert all symbol references in a class to direct references!

Symbol reference: is to represent a variable or a class in the form of a string, we can read
String a= "a"; Student stu=new Student ();
A,stu are symbolic references!
Direct reference: is the address translated according to the symbol reference!

2.3 Initialization

Loading is not initialized!
Initialization refers to instantiation! Create an instance of the Class!

Time to initialize:
01. Active references to classes
001.new an object of a class
002. Newinstance by Reflection ()
003. The parent class must be initialized before the subclass is initialized
02. Passive References to classes
001. Accessing static content through the class name
002. Calling static constants of a class also does not initialize the class
003. Class is not initialized when a class is used as an array of objects
004. Subclasses calling a static variable of a parent class does not load a static block of code for the subclass (the preparation phase of the class is not performed)

Three: Garbage collection mechanism (GC)

3.1: Why a garbage collection mechanism is required
01. As long as the object is created, it will open up space in the heap of the virtual machine;
02. When the program is running, n objects will be created, each object will have its own space;
03. If each object has a permanent possession of this memory space, obviously the memory is not enough!

In order to ensure that other objects can be created correctly!

In the C language, the task of garbage collection is the program ape itself responsible!
Problems that may occur:
01. Due to the carelessness of the procedural ape, not releasing the unused object in time, releasing the error!
02. The program Ape once released the program core objects (such as system objects), the system crashes!

3.2: Definition of garbage collection mechanism

During the Java program run, there is a special responsibility in the JVM to reclaim the memory of objects that are no longer in use!
This recycling process, we call it the garbage collection mechanism (gc,garbage Collection)!

3.3: Characteristics of garbage collection mechanism

01. Reduced the burden of the program ape in memory management;
02. Prevent system memory from being released illegally, making our program more robust;
03. The object can be recycled only if it is not referenced by any variable;
04. The program cannot force the garbage collector to recycle garbage immediately;
05. When the garbage collector is about to reclaim memory without objects, the object's Finalize () is called first
This method may cause the object to be resurrected, causing the garbage collector to cancel the collection of the object's memory.


3.4: The state of the object

In the Java Virtual Machine garbage collector, all objects in the heap have 3 states!

01. Accessible Status
02. Resurrection Status
03. Non-palpable status

Only when the object is in an inaccessible state will the garbage collection period really release the memory that the object occupies!


1. The life cycle of the object begins (new statement or reflection in Newinstance) accessible state
2. The object is not referenced, or the object invokes a finalize () resurrected state
3. The object called a Finalize () non-reachable state
4. Garbage collection
5. End of object life cycle

3.5: When garbage is recycled (conditions that trigger GC)

01. Object Not referenced = = = Object is in a non-reachable state
02. After the program has been properly executed in the scope
03.system.exit ()
04. Termination outside the program


3.6: Detailed GC
01. What time = = = Condition of triggering GC
001.SYSTEM.GC () may trigger;
002. The system itself determines the timing of the GC's triggering
Depends on the size of the memory in the Eden and from space areas!

02. What's the right thing?
Objects in Java = = = "Objects that cannot be found by parsing algorithms

03. What has been done
Copy operations on the objects that are being searched;
Finalize () is performed on objects that cannot be searched.


If you really recycle an object, you need to mark at least two times!
01. First token: For a time when there is no other object reference, execute Finalize ()
02. Second mark: For a filtered object, recycle.


Memory allocation mechanism in 3.7:java

Core: Generational distribution, generational recycling!

New generation (young Generation)
Eden Zone: is a contiguous memory space, so allocate memory very fast
Survivor Zone: From to must have an area that is empty =
older generation (old Generation)
Permanent generation (permanet Generation) = = = Method Area
reclaim two types of data:
01. Constants in a constant pool
02. Useless Class Information
When do I recycle?
01. All instances of the class are recycled
02. The ClassLoader of the loading class has also been recycled .
03. Class objects with classes are not referenced


Note the point:
01. The vast majority of objects created are assigned to the Eden area, but most objects will die in this area!
02. When the Eden area is full, the minor GC (young GC) is executed, and the dead object is cleaned out,
The remaining surviving objects are stored in the Survivor0 area (the Survivor1 area must be empty)
03. Each time the Eden area is full, the minor GC is executed, and all surviving objects are placed in the Survivor0 area.
04. When the Survivor0 area is full, it will copy the surviving objects to the Survivor1 area, then empty the Survivor0 area!
05. After repeated execution, the still-alive objects are copied to the old age!
06. The memory of the old age is larger than that of the younger generation, but it is full when the old age is full. Major GC
07. If the object is relatively large, the younger generation will not store it, and the object can be put into the old age!


The difference between the young GC and the full GC:
The 01.Young GC is triggered when the new generation of Eden is full and the Survivor area is full.
The full GC is triggered when the old age
High frequency of 02.Young GC execution
Low frequency of full GC execution

When to trigger full GC
01. Execute System.GC (), the system recommends performing full GC, but it is not necessary to perform
02. Lack of space in the old age
001. When young GC executes the surviving objects need to enter the old age, but found that the old age space is insufficient
002. When the Survivor0 area, the Survivor1 area to switch back and forth, found that the memory is not enough, must be put into the old age, and the old age space is insufficient
03. Insufficient Method Area space

3.8:GC the algorithms involved


01. Reference Counting Method Reference counting
Add a reference counter to the object, use a reference +1 at a time, and one less reference-1!
Recycle when the reference is 0!
But can't solve the problem of circular reference!

02. Root Search Algorithm GC Roots tracing
The object takes the root as a starting point, extends downward, and extends the path we call the reference chain (Reference Chain)
When an object does not have any reference chain connections, objects that are not available when proving this object (unreachable), will be recycled!

What is root?
01. Objects referenced by constants in the method area
02. Objects referenced in the stack
03. Objects referenced by static properties

03. Replication Algorithm Copying
is to divide the original memory space into two pieces, you can only use one piece at a time!
The disadvantage is to waste half of the memory space! Efficient than mark-clear algorithm!
Not suitable for use in areas where there are more surviving objects!
Used in the Cenozoic!

04. Mark-Clear Algorithm Mark-sweep

001. Mark all the surviving objects
002. Clear all unmarked objects uniformly

Disadvantages:
01. Two process efficiency is slow, because you need to query
02. Because memory fragmentation occurs during the purge process, if large objects cannot be stored

04. Labeling-Sorting algorithm mark-compact
Suitable for the survival of many areas of the object, so suitable for use in the old age!

  

Classes in 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.