python - 如何避免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
解决方案
推荐阅读
- android - YouTube之类的应用程序中BottomNavigationView中的两个片段之间的交易如何如此流畅?
- c++ - 将 char 数组复制到 QByte 数组中,而无需传递指针
- spring-webflow - Webflow 1 到 Webflow 2 迁移 - 警告:不再支持 bean 属性
- swagger - 将 OpenAPI 部署到本地主机
- ubuntu - Cmake外部共享库未在ubuntu上链接
- python - 为什么 [] 和 bool 返回 []?
- node.js - AWS::Lambda 无服务器调用本地函数不反映我的新 wcode
- php - 如何在同一查询中获取实体和计数?
- javafx - javafx SplitPane 分配器在内容/子项更改时跳转
- r - 如何使用两个日期之间的季度序列来旋转表格