首页 > 解决方案 > 如何避免kivy图形的内存问题?

问题描述

我遇到严重的记忆问题。每当绘制对象的数量达到 50 左右时,屏幕就会冻结并结束游戏。(请参阅下面的错误消息)

我正在使用 windows、python 3、kivy 和 geany

所有绘制的对象都被添加到一个列表中,许多元素(大约 50 个)会导致性能问题。我已经尝试使用垃圾收集( import gc, gc.collect() )来清理内存,但这大大降低了游戏的速度。

您可以在 Github 上找到我的代码以及有关游戏和问题的更多信息: https ://github.com/MartinPhilipp/Kivy-towers

# adding to the list:

  with self.canvas:
                b = random.choice(("rect", "rect", "rect", "circle", "ornament"))
                red,green,blue = random.random(), random.random(), random.random()
                Color(red,green,blue,1)
                if b == "rect":
                    x = random.randint(build_x-20, build_x+20)
                    size_x = random.randint(50,150)
                    size_y = random.randint(50,100)
                    Rectangle(pos=(x,y),size=(size_x,size_y))
                    p_list.append((b, x, y, red, green, blue, size_x, size_y))

# deleting from the list 

 def destroy(self, playernumber):
        for number, li  in enumerate((self.player1_list, self.player2_list)):
            if number != playernumber:
                continue
            items = len(li)
            if items > 0:
                for i in range(max(items//5,1)):
                    if len(Particle.zoo.keys()) < 10:
                        for x in range(3):
                            Particle(x=li[i][1],y=li[i][2],playernumber=playernumber)
                    del li[i]
            self.renew_screen()

这是错误消息:

MemoryError
Exception ignored in: 'kivy.graphics.instructions.VertexInstruction.radd'
MemoryError
MemoryError
Exception ignored in: 'kivy.graphics.instructions.Instruction.radd'
MemoryError
MemoryError
Exception ignored in: 'kivy.graphics.instructions.VertexInstruction.radd'
MemoryError
SystemError: Unable to realloc memory for free list
Exception ignored in: 'kivy.graphics.buffer.Buffer.grow'
SystemError: Unable to realloc memory for free list
SystemError: Unable to realloc memory for buffer
Exception ignored in: 'kivy.graphics.buffer.Buffer.grow'
SystemError: Unable to realloc memory for buffer

标签: pythonanimationmemorygraphicskivy

解决方案


推荐阅读