Construction method
- Constructors, also known as constructors, are used to create and initialize objects.
- Called through the new keyword.
- Although the constructor has a return value, the return value type cannot be customized (the return value type is this class), and return cannot be used in the constructor to return a value.
- If we do not define a constructor, the compiler will automatically define a parameterless constructor. If defined, the compiler does not automatically add.
- Constructor's method name must be consistent with the class name.
// Define a Point class class Point{ double x,y; // The result of the member variables x and y will be returned by default. Custom construction method. Constructor name and class name must be consistent. The result of the member variable is returned by default public Point(double _x,double _y){ x = _x; y = _y; } // Define class method: calculate the distance between two points // The p object defined by Point p here belongs to the getDistance() method, which is different from the p object in the main() method public double getDistance(Point p){ // Define the return value as the calculation result return Math.sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y)); } } public class TestCon { public static void main(String[] args) { // Class object defining the construction method: pass the corresponding parameters according to the actual situation // The p object defined by Point p here belongs to the main() method. It is different from the p object in the getDistance() method Point p = new Point(3.0,4.0); // Define an initial location Point orgin = new Point(0.0,0.0); // Output the operation result: transfer the origin to the getDistance() method for calculation, and finally print it out System.out.println(p.getDistance(orgin)); // The output is 5 } }Attention
Question (1): is the creation of objects completely implemented by construction methods?
No, construction method is an important way to create Java objects. When the constructor is called through the new keyword, the constructor does return this kind of object, but this object is not completely created by the constructor.
Question (2) there are four steps to create an object:
- Allocate object space and initialize the object member variable to 0 or empty.
- Perform display initialization of property values.
- Execute the construction method.
- Returns the address of the object to the relevant variable.
Note:
- Construction methods are often overloaded. Because construction methods are often called to create objects and so on.
- If the formal parameter name and attribute name in the constructor are the same, you need to use the this keyword to distinguish between attributes and formal parameters.
- As shown in the following code example: this.id represents the attribute ID, and ID represents the formal parameter ID
public class TestUser { int id; // id String name; // user name String pwd; // password // Define parameterless construction method public TestUser(){ } // Overloading of construction methods public TestUser(int id,String name){ // super(): when the compiler executes the construction method, it will execute super() first by default, no matter whether super() is written or not, and it will be added automatically. this.id = id; // this represents the created object this.name = name; // Output the passed in argument value. printf() is used to print out strings System.out.printf("%s,%s%n",id,name); } // Overloading of construction methods public TestUser(int id,String name,String pwd){ this.id = id; this.name = name; this.pwd = pwd; // Output the passed in argument value. printf() is used to print out strings System.out.printf("%s,%s,%s%n",id,name,pwd); } public static void main(String[] args) { TestUser user1 = new TestUser(); TestUser user2 = new TestUser(23,"Ah jun"); TestUser user3 = new TestUser(23,"WeChat official account","Ah jun Cultivation manual"); } }
Garbage collection mechanism
memory management- Java memory management largely refers to the management of objects, including the allocation and release of object space.
- Allocation of object space: use the new keyword to create objects.
- Release of object space: assign the object to null, and the garbage collection period is responsible for recycling the memory space of all "unreachable" objects.
Any garbage collection algorithm needs to do two basic things:
- Useless objects found.
- Reclaim memory space occupied by useless objects.
Garbage collection mechanism can ensure that "useless objects" are recycled. A useless object means that no variable references the object.
Java garbage collection machine finds useless objects through relevant algorithms, and makes them clear and sorted.
- Reference counting method
- Referential reachability (root search algorithm)
- ............
The generational garbage collection mechanism is based on the fact that:
- The life cycle of different objects is different.
- Objects with different life cycles can adopt different recycling algorithms to improve efficiency.
- The object is divided into three states: young generation, old generation and lasting generation.
- The JVM virtual machine divides the heap memory into Eden, Survivor, Tenured/Old spaces
-
Young generation:
All newly generated objects are placed in the Eden area.
The goal of the younger generation is to collect objects with a short life cycle, corresponding to Minor GC,
Each Minor GC will clean up the memory of the younger generation. The algorithm adopts a highly efficient replication algorithm and operates frequently, but it wastes memory space.
When the "younger generation" area is full of objects, the objects will be placed in the older generation area. -
Old generation:
Objects that are still alive after performing garbage collection N times (15 times by default) in the younger generation will be placed in the older generation.
Objects stored in older generations are generally objects with a long declaration cycle.
If there are more objects in the old generation, it is necessary to start Major GC and full GC (full recovery) for a major clean-up to comprehensively clean up the young generation and the old generation. -
Persistent generation
Persistent generation is used to store static files, such as Java classes, methods, etc.
Persistent generation has no significant impact on garbage collection.
- Minor GC
It is used to clean up the younger generation area. When Eden area is full, Minor GC will be triggered once
Clean up the useless objects and copy the useful objects into the "Survivor1" and "Survivor2" areas (the size and space of these two areas are the same. At the same time, only one Survivor1 and Survivor2 is in use and the other is empty). - Major GC
Used to clean the old generation area. - Full GC
It is used to clean up the areas of the younger and older generations, which has a high cost and will affect the performance.
Detailed garbage collection process
- Most of the newly created objects are stored in Eden.
- When the Eden area is full (reaching a certain proportion) and no new objects can be created, garbage collection (GC) will be triggered to clean up the useless objects, and then copy the remaining objects to the Survivor area. The Survivor1 area in the screenshot above will be emptied at the same time.
- When the Eden area is full again, objects that cannot be emptied in Survivor1 will be placed in another Survivor area, such as Survivor2.
- Repeat multiple times (15 times by default). Objects not cleaned up in the Survivor area will be copied to the old / tenured area.
- When the Old area is full, an integrity garbage collection (Full GC) will be triggered. The previous young generation garbage collection is called Minor GC.
JVM tuning and Full GC
When tuning the JVM, a large part of the work is to adjust the Full GC.
The following conditions may cause Full GC:
- The tenured generation is full.
- Perm is full.
- System.gc() is explicitly called (the program will not call it immediately when it encounters it. Writing this statement only suggests that the program start Full GC. The final time of calling depends on the garbage collection mechanism. This operation does not call Full GC)
- After the last GC (recycle), the allocation policies of each domain of the heap heap change dynamically.