程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python uses weak reference weakref to realize memory management of ring data

編輯:Python

1、 How to manage memory in a ring data structure ?

         Actual case :

                 stay python in , The garbage collector collects garbage objects by reference counting , But some ring data structures ( Trees , chart ...), There is a circular reference between objects , For example, the parent node of the tree refers to the child node , The child node also refers to the parent node . At the same time del Drop reference parent and child nodes , Two objects cannot be recycled immediately .

                 How to solve this kind of memory management problem ?

         Solution :

                 Use standard library weakref, It can create an object that can access the object without increasing the reference count .

2、 Code demonstration

(1) Garbage collection -- Introduction to reference counting and weak reference weakref Easy to use

import sys
class A(object):
def __del__(self):
# Define destructors , It will be called when the instance is recycled
print(' in __del__')
a = A()
# View the reference count of the object , The result is 2, Because when this function is called, it also passes in a
print(sys.getrefcount(a))
# Minus the 1 Times quoted
print(sys.getrefcount(a) - 1)
# take a Assign a value to a2 The reference count will add 1
a2 = a
print(sys.getrefcount(a) - 1)
# del It will be subtracted 1 Secondary reference count , When the reference count is 0 when , Objects will be recycled
del a2
# change a The direction of , To quote numbers 5, At this time, no reference is made to A(),
# The reference count is 0 The object is recycled to trigger the destructor .
a = 5
print(a)
a = A()
print(sys.getrefcount(a) - 1)
# Import weak references
import weakref
# Create a weak reference to an object
a_wref = weakref.ref(a)
# It is called just like a function , Now you get a A() References to
a2 = a_wref()
# You can see the simultaneous reference A() This object
print(a is a2)
del a
# A() The reference count changes to 0, Being recycled
del a2
# A weak reference returns an object reference when the object exists , If the object does not exist, return None
print(a_wref() is None)

(2) Examples are given to illustrate the circular reference problem and its solution

class Data(object): # Node data class
def __init__(self, value, owner):
# self.owner = owner
self.owner = weakref.ref(owner)
self.value = value
def __str__(self):
# return "%s's data, value is %s" % (self.owner, self.value)
return "%s's data, value is %s" % (self.owner(), self.value)
def __del__(self):
print('in __del--')
class Node(object): # Node class
def __init__(self, value):
# Circular reference
self.data = Data(value, self)
def __del__(self):
print('in Node.__del__')
node = Node(100)
# Circular references cannot be picked up immediately , Unknown points will be recycled in the future
del node
# Import gc Module forced recycling
import gc
# Use forced recycling , Nor can it be recycled immediately ,
gc.collect()
'''
This is the memory management problem of circular reference , To solve this kind of problem
You can use weak references in the standard library , A weak reference is an object that can be accessed
But do not increase its reference count .
'''
# quote weakref Weak reference modification owner Is a weak reference , The above code changes .
'''
Let the data owner It's right node Weak reference , This will not increase node Reference count of
This eliminates the problem of circular references , In the delete node When Data and Node Objects are recycled .
'''
node = Node(100)
del node
# Delete node Directly trigger the destructor of two instances , Running results :
in Node.__del__
in __del--


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved