本文介绍了Python垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一些在循环中创建对象的python代码,并且在每次迭代中用相同类型的新对象覆盖此对象。这是10.000次,Python每秒钟占用7MB的内存,直到使用我的3GB RAM。有没有人知道从内存中删除对象的方法?

I have created some python code which creates an object in a loop, and in every iteration overwrites this object with a new one of the same type. This is done 10.000 times, and Python takes up 7mb of memory every second until my 3gb RAM is used. Does anyone know of a way to remove the objects from memory?

推荐答案

您没有提供足够的信息 - 这取决于你正在创建的对象的具体细节,以及你在循环中做了什么。如果该对象不创建循环引用,则应在下一次迭代中解除分配。例如,代码

You haven't provided enough information - this depends on the specifics of the object you are creating and what else you're doing with it in the loop. If the object does not create circular references, it should be deallocated on the next iteration. For example, the code

for x in range(100000):
  obj = " " * 10000000

不会导致不断增加的内存分配。

will not result in ever-increasing memory allocation.

这篇关于Python垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 01:59