蟒蛇内存泄漏

发布于 2024-12-11 11:06:16 字数 1072 浏览 0 评论 0原文

我有一个像这样创建的“单元”对象数组:

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()

我疯狂地泄漏内存......发生了什么事?

I have an array of 'cell' objects created like so:

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)

I am grabbing attributes from them in a loop like so:

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()

I am leaking memory like crazy... what's going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

复古式 2024-12-18 11:06:16

PixelArray 中似乎存在错误。

以下代码通过在每个循环中设置一个像素来复制内存泄漏,您甚至不必更新显示即可导致问题:

import pygame, sys
from pygame.locals import *
ScreenWidth, ScreenHeight = 640, 480
pygame.init()
Display = pygame.display.set_mode((ScreenWidth,ScreenHeight), 0, 32)
pygame.display.set_caption('Memory Leak Test')
while True:
    PixelArray = pygame.PixelArray(Display)
    PixelArray[ScreenWidth-1][ScreenHeight-1] = (255,255,255)
    del PixelArray
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
#pygame.display.update()

如果您可以处理性能损失,则用(非常短的)行替换直接像素设置可以避免内存泄漏,即: pygame.draw.line(Display, Colour, (X,Y), (X,Y), 1)

There appears to be a bug in PixelArray.

The following code replicates the memory leak by setting a single pixel each loop, and you don't even have to update the display to cause the problem:

import pygame, sys
from pygame.locals import *
ScreenWidth, ScreenHeight = 640, 480
pygame.init()
Display = pygame.display.set_mode((ScreenWidth,ScreenHeight), 0, 32)
pygame.display.set_caption('Memory Leak Test')
while True:
    PixelArray = pygame.PixelArray(Display)
    PixelArray[ScreenWidth-1][ScreenHeight-1] = (255,255,255)
    del PixelArray
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
#pygame.display.update()

If you can handle the performance hit, replacing direct pixel setting with (very short) lines avoids the memory leak, ie: pygame.draw.line(Display, Colour, (X,Y), (X,Y), 1)

木格 2024-12-18 11:06:16

Cell 类可以通过使用槽来压缩(内存大小)。这应该会大大减少内存消耗:

class Cell(object):
    __slots__ = ('index', 'type', 'color')
    ...

代码中唯一的大型结构是 pixArr。您可能希望使用 sys.getsizeof 来监视其大小。另一个追踪泄漏的工具是漂亮地打印gc.garbage

The Cell class can be tighted-up (memorysize) by using slots. That ought to substantially reduce memory consumption:

class Cell(object):
    __slots__ = ('index', 'type', 'color')
    ...

The only large structure in your code is pixArr. You might want to monitor its size with sys.getsizeof. Another tool for tracking down leaks is to pretty print gc.garbage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文