Summary (3)---Knowledge Summary (memory management, thread blocking, Gil Lock)

Source: Internet
Author: User

How is memory management in Python?

  • Garbage collection: Unlike languages like C++,java, Python can assign variables directly without declaring the variable type beforehand. For Python, the type and memory of the object are determined at run time. That's why we call the Python language a dynamic type.
  • Reference count: Python manages memory in a manner similar to Windows kernel objects. Each object maintains a count of this reference to the object. When the variable is bound to an object, the reference count of the variable is 1, the system will automatically maintain these tags, and timed scan, when a tag's reference count becomes 0, the pair will be recycled
  • Memory pool mechanism Python's memory mechanism has pyramid rows, 1, 2 layers are mainly operating system operations
    • The No. 0 layer is the memory allocation and deallocation functions in C, such as Malloc,free.
    • Layers 1th and 2nd are memory pools, with Python's interface function Pymen--malloc function implemented, which allocates memory directly when the object is less than 256K
    • The 3rd layer is the topmost, the direct manipulation of our team's Python object.
  • In c if the frequent calls to malloc and free, is a performance problem, coupled with the frequent allocation and release of small chunks of memory will be fragmented, Python's main work here is:
      • Use a byte memory management system if the requested memory is used between 1~256 bytes, otherwise directly using malloc
      • This will still call malloc to allocate memory, but each time it allocates a chunk size of 256K chunks of memory.
      • The memory registered through the memory pool is eventually reclaimed to the memory pool and does not call C's free release. For the next use. For simple Python objects, such as numeric values, strings, tuples take the form of replication, that is, when another variable B is assigned to variable a, although the memory space of A and B is still the same, but when the value of a changes, a space is re-allocated to a, and the addresses of A and b become no longer the same

Ii. What are the common design patterns in Python?

1. Create pattern

    • Socialization of the Division of labor more and more thin, self-heating in the software design is the same, so the creation of objects and the use of objects is also an inevitable trend, because the creation of objects will consume a lot of resources of the system, so the creation of a separate object to study, so that can be funny to create the object is to explore the problem. There are 6 specific creation patterns available for research, namely:
      • Simple Factory mode (Factory)
      • Factory mode (Factory method)
      • Abstract Factory mode (Factory)
      • Creator mode (Builder)
      • Prototype mode (PROTOTYPE)
      • Singleton mode (Singleton)

Description: Strictly speaking, the simple factory model is not one of the 23 design patterns summed up by Gof

    • 2. Structural mode
      • After solving the problem of object creation, the composition of objects and the dependencies between objects become the focus of developers, because how to design the structure of objects, inheritance and dependencies will affect the maintenance of subsequent programs, code robustness, coupling and so on. The design of the object structure is very easy to show the level of the designer, there are 7 specific structural models to study, they are:
        • Appearance mode (facade)
        • Adapter Mode (Asapter)
        • Agent mode (proxy)
        • Decorative mode (Decorator)
        • Bridging mode (bridge)
        • Combination Mode (Composite)
        • Enjoy meta mode (Flyweight)

  • 3. Behavioral mode
      • After the object section and object creation are resolved, the behavior of the object remains, and if the object behaves well, the object will behave more clearly and the collaboration between them will improve, and there are 11 specific behavioral patterns to be studied, namely:
        • Template method Mode
        • Observer Mode (OBSERVER)
        • Status mode (state)
        • Policy mode (strategy)
        • Responsibility chain mode (Chain of Responsibility)
        • Command mode
        • Visitor Mode (Visitor)
        • Mediator Mode (mediator)
        • Memo Mode (Memento)
        • Iterator mode (Iterator)
        • Interpreter mode (interpreter)

Third, what is thread safety?

Thread safety is in a multi-threaded environment, can ensure that multiple threads execute simultaneously while the program is still running correctly, and to ensure that the shared data can be accessed by multiple threads, but only one thread at a time to access. The solution to the problem of resource competition in multi-threaded environment is locking to ensure the uniqueness of access operation.

Iv. What is the effect of the Gil lock on Python multithreading?

The full name of the Gil is the global interpreter lock, which is the source of the Python design considerations and future data security decisions. Each CPU executes only one thread at a time. (In a single-core CPU, multithreading is actually only concurrency, not parallel, concurrency and parallelism are both the concept of processing multiple requests at the same time, but concurrency and parallelism are different, parallel refers to two or more times at the same time, and concurrency refers to two or more events occur at the same interval)

In Python multithreading, the way each thread executes:

    1. Get Gil
    2. Execute code until sleep or a Python virtual machine suspends it
    3. Release Gil

It can be seen that a thread wants to execute, must first get the Gil, we may think of the Gil as a "pass", and in a Python process, Gil only with one, cannot get the pass of the thread, do not allow access to the CPU execution

In python2.x, the Gil's release logic is that the current thread meets the IO operation or the ticks technology reaches 100 to release, and each time the Gil Lock is freed, the thread locks up and switches threads, consuming resources. And because the Gil Lock exists, a process in Python can always execute only one thread at a time (the thread that gets the Gil can execute)

IO-Intensive code (file processing, web crawler, etc.) multithreading can effectively improve efficiency (single-threaded IO operation will be IO wait, resulting in unnecessary time wasted, and turn on multithreading can do thread a wait, automatically switch to thread B, can not waste CPU resources, which can improve program execution efficiency ), so multithreading is more friendly to IO-intensive code

V. What is blocking? What is non-blocking?

The current thread is suspended until the call results are returned by a blocking call. Functions are returned only after the result is obtained. Someone might put blocking calls and synchronous calls together, in fact he is different. For synchronous calls, many times the current thread is still active, but logically the current function does not return, for example, we call the receive function in the socket, and if there is no data in the buffer, the function waits until the data is returned. At this point, the current thread will also continue to process a variety of messages, if the main window and the calling function are in the same thread, unless you call in a special interface operation function, the main interface should still be able to refresh. Another function that the socket receives data recv is an example of a blocking call, and when the socket is working in blocking mode, if the function is called without data, the current thread will be suspended until there is data

The concept of non-blocking and blocking corresponds to a function that does not block the current thread until the result is immediately available, and returns immediately

What is the difference between a soft connection and a hard link?

Soft Connect is like a shortcut to Windows, and when you delete a source file, the soft connection fails. A hard link can be understood as an alias for the source file. Multiple aliases represent a single file. RM a file, then the number of hard links in this file is reduced by one, and when the number of hard links is 0, the file is deleted

What are the application scenarios of the singleton mode?

Scenarios where a singleton pattern is applied are generally found under the following conditions:

(1) In the case of resource sharing, avoid the performance loss caused by resource operation. such as log files, application configuration.

(2) In the case of resource control, facilitate communication between resources. such as the thread pool.

    • Counter of the website
    • Application Configuration
    • Multi-threaded Pool
    • Database configuration, database connection pool
    • application's log Application

Summary (3)---Knowledge Summary (memory management, thread blocking, Gil Lock)

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.