Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of the principle of garbage Collection Mechanism developed by python language

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the example analysis of the principle of garbage collection mechanism developed by python language, which is very detailed and has certain reference value. Friends who are interested must read it!

one。 What is garbage collection mechanism?

Garbage collection mechanism (GC for short), a mechanism included in the interpreter

It is a dynamic storage management technology that automatically releases the memory space occupied by objects that are no longer referenced by the program.

two。 Why is there a garbage collection mechanism?

A large amount of memory space will be applied for during the running of the program.

For some useless space, if it is not cleaned in time, it will lead to memory overflow (insufficient), and the program will crash.

Managing memory is very complex, and the garbage collection mechanism frees programmers from complex memory management.

three。 The principle of garbage collection mechanism 1. Reference count

Reference counting is the number of times a variable name is associated with a variable value to track and collect garbage

Direct reference

Directly referenced by variable name

X = 18 # 18 referenced once, counted as 1y = x # 18 referenced plus 1, counted as 2z = y # 18 referenced plus 1, counted as 3print (id (x)) # 140725488808736print (id (y)) # 140725488808736print (id (z)) # 140725488808736 indirect reference

Container references to it are all indirect

X = 18 # 18 is referenced once, counted as 1li = [1 age': 2dhox] # through list reference, count plus 1, 2dic = {'age': x} # through dictionary reference, count plus 1, 3print (id (x)) # 140725486514976print (id (li [2])) # 140725486514976 list reference Count 4print (id (dic ['age'])) # 140725486514976 dictionary references, count 52. Stack area / stack area

Stack area: the memory address mapping relationship between variable name and variable value is stored

Stack area: the real location of the value is stored.

3. Summary

Direct reference refers to the memory address referenced directly from the stack area.

Indirect reference refers to the memory address that is referenced from the stack area to the heap area and then reached by further reference.

four。 Mark clear 1. Circular reference problem (also known as cross-reference) # Let's first define list L1 = [0] # listing 1 is referenced once, the reference count of listing 1 becomes 1 L2 = [1] # listing 2 is referenced once, and the reference count of listing 2 becomes 1 # add the list to another list l1.append (L2) # append listing 2 to L1 as the second element The reference count in listing 2 becomes 2l2.append (L1) # appends listing 1 to L2 as the second element, and the reference count in listing 1 changes to the deregulation variable name "L1" and the corresponding relationship between "L2" and value del l1del L22. The result of circular reference

The value is no longer associated by any name, but the reference count of the value is not 0

Should be recycled, but cannot be recycled.

3. Workaround: clear-Mark

References to container objects may produce circular references, and clearing-tags is to solve this problem.

When the free space of the application is exhausted, the clear-tag stops the entire program, then marks it first, and then clears it

Marking

However, if you can start from the stack area and find the contents of the corresponding stack area (direct or indirect reference), you can mark it to survive, and if it is not alive, clear it.

Specifically: the process of marking is actually traversing all "GC Roots" objects (all content or threads in the stack area can be used as "GC Roots" objects)

Then mark all objects that can be accessed directly or indirectly by "GC Roots" as living objects, and the rest are non-living objects and should be cleared.

Clear

Traverse the objects in the heap and clean up all the objects that are alive without tags

five。 Generation by generation recovery 1. Efficiency problem

Based on the recycling mechanism of reference counting, every time memory is reclaimed, it is necessary to traverse the reference count of all objects.

This is very time-consuming, so generational recycling is introduced to improve the efficiency of recycling.

Generation-by-generation recycling uses the strategy of "space for time".

two。 Solution: generation by generation, recovery by generation

Generation refers to grading variables according to their survival time.

If a variable is often quoted, the level (weight) will be increased, and when the weight reaches the set value, it will move on to the next level.

When it is not recycled after many scans, the "GC mechanism" will assume that the variable is constant.

As a result, the scanning frequency will be reduced.

Recovery

When the count decreases, it is easy to be recycled.

Generational recycling can improve efficiency, but it also has some disadvantages:

For example, as soon as a variable changes from a low level to a high level, it is unbound.

It should be recycled, but the high-level scanning frequency is lower than the low-level scanning frequency.

Then this unbound variable cannot be cleaned up in time.

The above is all the contents of the article "sample Analysis of the principle of garbage Collection Mechanism developed by python language". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report