Python内存类对象函数

Python内存类对象函数,python,function,class,object,memory,Python,Function,Class,Object,Memory,为什么下面代码中的函数“update_wts”会占用内存。当函数存在时,为什么内存不返回 该函数将对象变量中的1替换为1,那么为什么要添加内存呢?为什么退出功能后内存没有释放 class myClass(object): def __init__(self,i): self.ID = i self.wts = {} self.prc = {} self.numbers = [] self.n = 1000

为什么下面代码中的函数“update_wts”会占用内存。当函数存在时,为什么内存不返回

该函数将对象变量中的1替换为1,那么为什么要添加内存呢?为什么退出功能后内存没有释放

class myClass(object):
    def __init__(self,i):
        self.ID = i
        self.wts = {}
        self.prc = {}
        self.numbers = []
        self.n = 1000

    def initial_values(self):
        self.numbers = range(self.n)
        for i in self.numbers:
            self.prc[i] = 1

    def update(self):
        for i in self.numbers:
            self.prc[i] = 1


how_many = 100000

classObjects = [myClass(i) for i in xrange(how_many)]

for ob in classObjects:
    ob.initial_values()

@profile
def update_wts():
    for ob in classObjects:
        ob.update()


update_wts()

使用python-m memory\u profiler test.py查看内存,将代码放在test.py中

您的代码从不调用
初始值
,因此它不会真正执行任何操作。谢谢。增加初始_值;仍然使用内存