【发布时间】:2011-12-13 14:16:42
【问题描述】:
我有一个像这样创建的“单元”对象数组:
class Cell:
def __init__(self, index, type, color):
self.index = index
self.type = type
self.score = 0
self.x = index%grid_size
self.y = int(index/grid_size)
self.color = colour
alpha = 0.8
b = 2.0
grid_size = 100
scale = 5
number_cells = grid_size*grid_size
num_cooperators = int(number_cells*alpha)
cells = range(number_cells)
random.shuffle(cells)
cooperators = cells[0:num_cooperators]
defectors = cells[num_cooperators:number_cells]
cells = [Cell(i, 'C', blue) for i in cooperators]
cells += [Cell(i, 'D', red) for i in defectors]
cells.sort(compare)
我在这样的循环中从它们那里获取属性:
while 1:
pixArr = pygame.PixelArray(windowSurfaceObj)
for cell in cells:
x = cell.x
y = cell.y
color = cell.color
for y_offset in range(0,scale):
for x_offset in range(0,scale):
pixArr[x*scale + x_offset][y*scale + y_offset] = color
del pixArr
pygame.display.update()
我正在疯狂地泄漏内存......这是怎么回事?
【问题讨论】:
-
你如何确定你正在泄漏内存?
-
我注意到我的机器上播放的音乐开始跳动,所以我查看了我的系统活动。它从大约 100MB 的内存使用量开始,但很快就会达到 1 GB 或 2!
-
那么
number_cells和num_cooperators呢? -
我猜你显示的循环是在另一个循环中世代相传。您是否为每一代存储
pixArr? -
请查看更新后的帖子,了解更多详情
标签: python memory-management memory-leaks pygame