1. The difference between TCP and UDP
Reference counting Principle: When an object's reference is created or copied, the object's reference count is incremented by 1; when an object's reference is destroyed, the object's reference count is decremented by 1, and when the object's reference count is reduced to 0, It means that the object is no longer used and its memory can be released.
Advantages: Reference counting has a great advantage, that is, real-time, any memory, once there is no reference to it, it will be immediately reclaimed, and other garbage collection techniques must be under certain special conditions to recover invalid memory .
Disadvantages: But it also has weaknesses. The additional operations of maintaining the reference count brought by the reference counting mechanism are directly proportional to the memory allocation and release performed by Python running, and the number of reference assignments is obviously higher than that of other garbage collection techniques. The additional operations brought about are only low in efficiency related to the amount of memory to be reclaimed. At the same time, there is another big problem with reference technology-circular references. Because objects refer to each other, the reference of each object will not be 0, so the memory occupied by these objects will never be released.
Mark-clear Mark-clear only focuses on objects that may cause circular references. Obviously, it is impossible for immutable objects such as PyIntObject and PyStringObject to generate circular references, because they cannot hold references to other objects. Circular references in Python always occur between container objects, that is, objects that can hold other objects internally, such as list, dict, class, and so on.
Principles: 1. Look for the collection of root objects as the starting point for garbage detection. The root objects are some global references and references in the function stack. The objects pointed to by these references cannot be deleted; 2. From root Starting from the object collection, following each reference in the root object collection, if an object can be reached, it means that the object is reachable and will not be deleted. This process is the garbage detection stage; 3. When the detection stage After the end, all objects are divided into reachable and unreachable parts, all reachable objects are reserved, and the memory occupied by other unreachable objects will be recycled. This is the garbage collection stage. (The bottom layer uses a linked list to connect the objects of these collections together)
Disadvantages: The marking and removal process is not efficient.
Generational collection Principle: All memory blocks in the system are divided into different collections according to their survival time. Each collection becomes a "generation". Python defines three generations of object collections by default. The frequency of garbage collection increases with the "generation". "The survival time increases and decreases. In other words, the longer the object is, the less likely it is to be garbage, and the frequency of garbage collection should be reduced. So how to measure this survival time: It is usually measured by several garbage collection actions. If an object has been garbage collected more times, it can be concluded: the longer the survival time of the object.
A process is a dynamic execution process of a program on a data set. The process consists of three parts: program, data set, and process control block. The program is used to describe which functions of the process and how to complete; the data set is the resource used in the execution of the program; the process control block is used to save the state of the program.
Multiple threads can be opened in one process, why should there be processes instead of threads? Because threads share a set of data in a program, if they are all made into processes and each process occupies a piece of memory, then this set of data will have to be copied to each program several times, which is unreasonable, so there are threads.
The relationship between process threads (1) A thread can only belong to one process, and a process can have multiple threads, but at least one thread is allocated to the process. The process is the main body of the program. All threads in the same process share this All resources of the process (3) CPU is allocated to the thread, that is, the thread that really runs on the CPU (4) The thread is the smallest unit of execution, and the process is the smallest unit of resource management
Coroutine, also known as micro thread, fiber. The coroutine seems to be a subroutine, but in the process of execution, it can be interrupted inside the subroutine, and then switch to execute other subroutines, and then return to continue execution when appropriate.
Recommended Posts