Detailed explanation of Java construction method / garbage collection mechanism / brief description of JVM tuning / memory leak

Construction method Constructors, also known as constructors, are used to create and initialize objects. Detailed expl...
Construction method
Garbage collection mechanism
Detailed garbage collection process
JVM tuning and Full GC
Operations that are easy to cause memory leakage during development (briefly understand first)

Construction method

  1. Constructors, also known as constructors, are used to create and initialize objects.
Detailed explanation of construction method (need to master)
  1. Called through the new keyword.
  2. 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.
  3. If we do not define a constructor, the compiler will automatically define a parameterless constructor. If defined, the compiler does not automatically add.
  4. Constructor's method name must be consistent with the class name.
Code example: use of construction method
// 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:

  1. Allocate object space and initialize the object member variable to 0 or empty.
  2. Perform display initialization of property values.
  3. Execute the construction method.
  4. Returns the address of the object to the relevant variable.
Construct method overloading (same as method overloading)

Note:

  1. Construction methods are often overloaded. Because construction methods are often called to create objects and so on.
  2. 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.
  3. As shown in the following code example: this.id represents the attribute ID, and ID represents the formal parameter ID
Code example: constructor overload
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
  1. Java memory management largely refers to the management of objects, including the allocation and release of object space.
  2. Allocation of object space: use the new keyword to create objects.
  3. 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.
Garbage collection process

Any garbage collection algorithm needs to do two basic things:

  1. Useless objects found.
  2. 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.
Garbage collection algorithm
  1. Reference counting method
  2. Referential reachability (root search algorithm)
  3. ............
Generational garbage collection mechanism algorithm (general)

The generational garbage collection mechanism is based on the fact that:

  1. The life cycle of different objects is different.
  2. Objects with different life cycles can adopt different recycling algorithms to improve efficiency.
  3. The object is divided into three states: young generation, old generation and lasting generation.
  4. 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.

Heap memory partitioning details
  • 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

  1. Most of the newly created objects are stored in Eden.
  2. 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.
  3. When the Eden area is full again, objects that cannot be emptied in Survivor1 will be placed in another Survivor area, such as Survivor2.
  4. Repeat multiple times (15 times by default). Objects not cleaned up in the Survivor area will be copied to the old / tenured area.
  5. 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:

  1. The tenured generation is full.
  2. Perm is full.
  3. 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)
  4. After the last GC (recycle), the allocation policies of each domain of the heap heap change dynamically.

Operations that are easy to cause memory leakage during development (briefly understand first)

3 November 2021, 19:33 | Views: 3984

Add new comment

For adding a comment, please log in
or create account

0 comments