Thinking about the garbage collection mechanism of Python

Source: Internet
Author: User
Tags server memory

First, preface

Python is a high-level language, used similar to natural language, the development of nature is very convenient and fast, because Python in the back for us to do a lot of things silently, one of which is garbage collection, to solve memory management, memory leak problems.

Memory leak: When the program is running, some objects do not work, but the occupied memory is not released, the server memory with less time, resulting in a system crash, so memory leak is a need to focus on the issue.

Second, reference counting

The way in which Python flags an object is also useful is to use a reference count, and the following scenario will count + 1 for that object:

1. When created

2. When quoted

3. When you pass in a function as a parameter

Instead, the following scenario is the count of the object-1:

1. By Del

2. Being re-referenced

3. Function execution Complete

Viewing the count of an element can be done by Sys.getrefcount (), and when the reference count is 0, the memory is freed.

It can be thought that compared with other garbage collection, Python mechanism advantage is obvious, is real-time, Python's GC module is an open interface for management.

It can also be easily guessed that the disadvantage is that performance is relatively low, read the report, Instagram by disabling the GC module, performance increased by 10%!

Third, circular reference

There is a special case where two or more variables are referred to each other, and the mechanism by which the count is referenced cannot be processed.

  

A == []a.append (b) B.append (a)print(b)

A, B has a reference count of 2 and cannot reclaim both memory

Iv. Solutions

1. Use "mark-clear" to resolve circular call issues:

The garbage collector periodically looks for such circular calls and clears

The first is to start looking from the copy of the root object collection, these objects count not 0, not cleared

And then a detection, divided into objects and unreachable objects, the bottom layer through the data structure of the linked list, through the operation of the copy to clear the mark, in the case of no impact on the original data, to determine whether it is a circular call

Finally, the unreachable object is cleared, freeing the memory, and the efficiency is low.

There are three scenarios that trigger garbage collection:
1. Call gc.collect() ,
2. When the counter of the GC module reaches the threshold.
3. When the program exits

2. Generational recycling, using the "space-for-time" strategy to improve efficiency:

Some memory blocks live from start to finish, others are short, so it's a waste of time to recycle them,

All objects begin to be divided into zero generations, Python has three generations by default, and one generation is a linked list

The younger generation of the object priority processing, experience the more garbage processing times, the more "seniority", will rise, eventually put in the second generation.

    

Note:

The garbage collection mechanism of Python is determined by whether the number of detections reaches the threshold value.

Python this aspect of the source code is written in C, temporarily do not understand, left to understand the chain list structure to study,

GC modules are reserved for later study.

  

    

Thinking about the garbage collection mechanism of Python

Related Article

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.