2022.1.4Java Basics - Construction Method, Memory Management

constructor overloading

With ginseng without ginseng

public class Point {
    double x,y;

    Point(double _x,double _y){
        x=_x;
        y=_y;
    }

    Point(){

    }
}

Java virtual machine memory model

The memory of the virtual machine can be divided into three areas: stack stack, heap heap, method area method area

stack:

The method to be executed or being executed is placed on the stack

  1.  The virtual machine allocates a stack frame for each method that enters the stack
  2. The virtual machine creates a stack for each thread to store the information of the method (local variables, actual parameters)
  3. The stack is private to the thread and cannot be shared between threads
  4. The storage characteristics of the stack: "first in first out, last in first out"
  5. The stack is automatically allocated by the system, and the speed is fast. The stack is a continuous memory space

heap:

The created objects and arrays are placed in the heap (arrays are also objects)

  1. The virtual machine has only one heap, which is shared by all threads
  2. The storage method of the heap in memory is discontinuous, so the allocation is flexible and the speed is slow
  3. The heap is shared by all threads, and many objects are created. The garbage collector will make further planning to recycle objects that have not been used for a long time, and then divide them into young generation and old generation.

Method area:

The only entity code is placed in the method area, that is, various blueprints (classes), constant pools, static classes, methods

  1. The method area is the specification of javaJVM, which can have different implementations
    1. JDK7 used to be the "permanent generation"
    2. JDK7 partially removes the "permanent generation", and moves static variables and string constant pools to the heap memory
    3. JDK8 is a combination of "metadata space" and heap, but it still exists
  2. JVM has only one method area, which is shared by all stacks
  3. The method area is actually a heap, which only stores information related to classes and constants
  4. Store the content that never changes, such as class information, static variables, strings, constants, etc.
  5. The constant pool mainly stores: strings, final constant values

garbage collection mechanism

Java introduces a garbage collection mechanism to improve development efficiency, and programmers do not need to pay too much attention to memory management mechanisms

memory management

To a large extent, memory management is: the management of objects in the heap, including the allocation and release of object space

Allocation of object space: use the new keyword to create objects

Release: Just assign the object to null

garbage collection process

Any garbage collection algorithm generally does two things:

  1. found useless objects
  2. Reclaim space occupied by unused objects

The discovery is that the garbage collector of java discovers useless objects through related algorithms and cleans them up.

Garbage Collection Related Algorithms

  1. reference counting algorithm
  2. Reference reachable method (root search algorithm): that is, starting from a node GC ROOT (the object that is alive at the current moment ), find the corresponding reference node, and then find the reference node on the found node until all nodes are not referenced , other nodes are regarded as useless nodes.

Briefly talk about the reference counting algorithm

That is, each object created has a reference count. Every time a variable refers to the object, the reference count is increased by 1, and if it is dereferenced (null), the reference count is -1.

Special case: this kind of garbage cannot be recognized

Objects AB refer to each other, even if ObjA and B stop referencing outside, but they are referencing each other instead, and cannot be recognized

 General Band Garbage Mechanism

 This is something in the heap. Now the Edem area stores objects, executes a Minor GC to clean up the young generation, and then the useless objects in the Edem area will be cleaned up, and the useful objects will go to the next survivor area, and then execute Minor in the 1 area GC, clear useless objects and transfer to District 2, and then transfer back to District 1. For more than 15 consecutive times, it will be transferred to the old generation and cleaned up by Major GC

Full GC cleans up the young generation, the old generation, and the permanent generation (which store class information, static areas, etc.)

memory leak

It means that the program in the heap memory is not released for some reason, resulting in waste of memory, resulting in slower running speed or even system crash

reason

1. Create a lot of useless objects

 2. The use of static collection classes

It is a large number of static tags, and these object or method properties will not be released for a long time

3. Various connection objects (IO stream objects, database connection objects, network connection objects) are not closed

4. Improper use of listeners

other points

  1. Programmers do not have permission to invoke the garbage collector
  2. Programmers can call System.gc(), which just notifies the JVM, and does not run the garbage collection period (Full GC). Use as little as possible, and will apply for Full GC, which is costly and affects system performance
  3. The finalize method of the Object object is a method provided by Java for programmers to release objects or resources, but it should be used as little as possible.

this

There is a this in each object, which stores the address of its own object. Calling this.property is to call itself, which will be used in the method, and the incoming parameters are copied to this.property

static

 

Attributes and methods with static tags belong to classes, attributes will not be created with the creation of objects, and will be automatically created at the beginning of the program. Static attributes and methods will be included in the class information

static initial block

public class TestStatic {
    int id;
    String name;

    static String company = "北京尚学堂";
    static {      
                                          
    //通过static{}来对静态属性初始化赋值

        System.out.println("执行类的初始化工作");
        company="腾讯山西太原分部";
        printCompany();
    }
    public TestStatic(int id,String name){
        this.id=id;
        this.name=name;
    }

    public void login(){
        System.out.println(name);
    }

    public static void  printCompany(){
        //login();
        System.out.println(company);
    }

    public static void main(String[] args) {
        TestStatic u = new TestStatic(101,"张小八");
        TestStatic.printCompany();
        TestStatic.company="北京腾讯分部";
        TestStatic.printCompany();
    }
}

Classification and scoping of variables

 

The core difference between local variables, member variables, and static variables
type declaration position subordinate to Life cycle (scope)
local variable inside a method or statement block method/block method call ends

Member variables

(instance variable)

Inside a class, outside a method object

The object is created, and the member variables are created accordingly

The object disappears, and the member variables disappear with it

static variable  

(class variable)

Inside the class, static modification kind The class is loaded, and the static variable is valid

packet mechanism

It is a folder, which is used to manage classes and solve the problem of duplicate names

package point;

 final keyword

 Know Object.toString(), rewrite

public class TestObject extends Object{
    String name;
    String pwd;

    @Override
    public String toString() {
        return  "name='" + name +", pwd='" + pwd ;
    }

    public static void main(String[] args) {
        TestObject a = new TestObject();
        System.out.println(a);
    }
}

==和Object.equals()

"==" represents whether the two parties are the same, if it is a basic type, the value is equal, if it is a reference type, the address is equal, it means that they are the same object.

equals() is the same as == by default, but rewriting this method, such as comparing whether all attributes of two objects are the same, means that the two objects are equal.

keyword super

 .

First call the static() of the parent class subclass;

 access modifier

 

 

 

interface

After jdk8, interfaces allow default methods and static methods

default method

public interface TestDefault {
    void printInfo();

    default void more(){
        System.out.println("more,测试默认方法");
    }
}


class TestDefault1 implements TestDefault{


    @Override
    public void printInfo() {
        System.out.println("通过接口实现的方法");
    }

    @Override
    public void more() {
        System.out.println("我重写了more默认方法");
    }
}

Guess you like

Origin blog.csdn.net/qq_41302243/article/details/122298894