- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在以某种方式构建一个相当复杂的 wxPython 应用程序,使用 ode 进行物理建模,使用 openGL 进行渲染,使用 wx 进行 UI。一切都进展顺利,直到应用程序开始崩溃。经过几天毫无进展后,我终于注意到我的应用程序正在泄漏内存。我能够将一些以相当惊人的速度泄漏的东西提炼成一个很小的示例脚本:
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
import wx
import wx.propgrid as wxpg
import random
class CoordProperty(wxpg.PyProperty):
def __init__(self, label, name, value=(0,0,0)):
wxpg.PyProperty.__init__(self, label, name)
self.SetValue(value)
def GetClassName(self):
return "CoordProperty"
def GetEditor(self):
return "TextCtrl"
def ValueToString(self, value, flags):
x,y,z = value
return "%f,%f,%f"%(x,y,z)
app = wx.App(False)
frame = wx.Frame(None, -1, "Test")
pg = wxpg.PropertyGridManager(frame)
props = {}
for i in range(1000):
prop_name = "prop_%d"%i
prop = CoordProperty("Coord", prop_name)
pg.Append(prop)
props[prop_name] = prop
def OnTimer(event):
global props
for key in props:
props[key].SetValue((random.random(), random.random(), random.random()))
timer = wx.Timer(frame, 1)
frame.Bind(wx.EVT_TIMER, OnTimer)
timer.Start(10) # 100Hz
frame.Show()
app.MainLoop()
timer.Stop()
该示例创建一个框架,并将 wxPropertyGrid 放入其中。它派生出一个显示 3D 坐标值的属性,创建一千个坐标值,然后通过以 100Hz 运行的计时器将每个坐标值更新为随机值。其泄漏速度接近 10Mb/秒,并最终崩溃。它通常也会在关闭时崩溃。
我在 Windows 7 上使用 python 2.7 和 wx 2.9.3.1 msw(经典)。
如果我用内置属性(例如 wxpg.FloatProperty)替换派生的 CoordProperty,并相应地修改代码,泄漏就会消失。
有什么想法吗?或者我应该提交一个wx bug?我什至可以删除派生属性类中函数 ValueToString 的定义,但应用程序仍然会泄漏。
最佳答案
我使用以下代码来计算对象:
def output_memory():
d = defaultdict(int)
for o in gc.get_objects():
name = type(o).__name__
d[name] += 1
items = d.items()
items.sort(key=lambda x:x[1])
for key, value in items:
print key, value
发现你的程序每次事件都会增加1000个元组。因此,当您调用 props[key].SetValue()
时,gc 尚未收集上一个值。这可能是wxpg的一个bug,我们可以通过使用([x],[y],[z])
来保存值来解决这个bug,这样你就可以更新值而无需调用设置值():
for name, prop in props.iteritems():
value = prop.GetValue()
value[0][0] = random()
value[1][0] = random()
value[2][0] = random()
pg.Refresh()
完整代码如下:
import wx
import wx.propgrid as wxpg
from random import random
import gc
from collections import defaultdict
def output_memory():
d = defaultdict(int)
for o in gc.get_objects():
name = type(o).__name__
d[name] += 1
items = d.items()
items.sort(key=lambda x:x[1])
for key, value in items:
print key, value
class CoordProperty(wxpg.PyProperty):
def __init__(self, label, name):
wxpg.PyProperty.__init__(self, label, name)
self.SetValue(([0],[0],[0]))
def GetClassName(self):
return "CoordProperty"
def GetEditor(self):
return "TextCtrl"
def GetValueAsString(self, flags):
x,y,z = self.GetValue()
return "%f,%f,%f"%(x[0],y[0],z[0])
app = wx.App(False)
frame = wx.Frame(None, -1, "Test")
pg = wxpg.PropertyGridManager(frame)
props = {}
for i in range(1000):
prop_name = "prop_%d"%i
prop = CoordProperty("Coord", prop_name)
pg.Append(prop)
props[prop_name] = prop
def OnTimer(event):
for name, prop in props.iteritems():
value = prop.GetValue()
value[0][0] = random()
value[1][0] = random()
value[2][0] = random()
pg.Refresh()
#output_memory()
timer = wx.Timer(frame, 1)
frame.Bind(wx.EVT_TIMER, OnTimer)
timer.Start(10)
frame.Show()
app.MainLoop()
timer.Stop()
关于python - wxpython 应用程序中严重的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10610570/
目前我正在调试我们的应用程序一个看似随机的错误。我将尝试提供尽可能多的细节。基本上过了一会儿,我们服务器上的应用程序的一个应用程序池停止了。我必须启动它,或者有时,重新启动 Coldfusion 或更
在寻找其他东西时,完全出于巧合,我偶然发现了一些关于案例类继承是多么邪恶的评论。有一个叫做 ProductN 的东西,可怜虫和国王, Sprite 和巫师,以及案例类继承如何丢失某种非常理想的属性。那
我正在从服务器下载媒体内容,并且应用程序在一定时间后崩溃,并给出以下错误: 收到内存压力通知(严重) 当我在 didReceiveMemoryWarning 函数中收到警告时删除了所有对象,但没有运气
为什么我会遇到这个错误 我开发了 Jax-Rs Web 服务 Jersey、Maven、Hibernate 和 Spring。如何解决该错误? Aug 03, 2015 2:23:17 PM com.
我是 Perl 新手。我收到此错误: CRITICAL : "file does not exist" 当我尝试运行脚本来通过 Nagios 监视 httpd 错误日志时。 #!/bin/sh # C
我最近一直在努力在 Kivy 中创建一个填充的 Circle,当窗口的大小调整为不同的宽度或高度时,它仍然是一个圆。我在这里看了问题: Centering an object in Kivy 但是当我
我想用实验编程语言实现的主要目的之一是:发生错误(语法,名称,类型等)时,无论程序有多严重或破坏性如何,都可以保持程序运行。 我知道这可能非常糟糕,但是我只想要一些不会在每个错误中都消失的东西-,我发
我使用 NetBeans 来制作我的 Java 项目。在我的数据库表或 DTO 中,它们都有一个名为“paymentDate ”的列。我不明白为什么它会发送此错误。奇怪的是,当我将“PAYMENTDA
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
我正在使用Grails 1.3.4,后者使用了Hibernate,并且针对Oracle 11g数据库。我的应用程序启动失败,出现以下错误: 2010-11-04 09:45:34,671 INFO
自从我执行了 pip install google-api-python-client 后,我的 Gunicorn worker 在超时后停止了。 Django==1.5.3 unicorn ==
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Connecting to SQLite Database Fails 我是 Java 初学者 当我想从 SQLit
在我的实体类中,我有以下内容,调用一个返回 sys_refcursor 的 oracle 函数 @Entity @javax.persistence.NamedNativeQuery(
我们在生产环境中遇到了这个问题,但无法追踪。技术:JSP/Servlets,Jetty 服务器版本:jetty-distribution-8.1.15, 以下是异常(exception)情况,我们在
我有一个基于网络的应用程序,每三秒我连接到一个数据库并从 GPS 设备查询数据。 不幸的是,当应用程序工作超过几个小时时我无缘无故地得到了异常:“严重:没有找到适合jdbc的驱动程序......”。应
之前我的 tomcat 服务器运行良好。 但突然出现以下错误。 请帮忙解决这个问题 SEVERE: A child container failed during start java.util.co
请不要标记为重复,因为其他解决方案还没有帮助! 我在尝试运行本教程时在浏览器中收到 404: http://www.vogella.com/articles/REST/article.html 实际上
使用 Spring Tool Suite 3.6.3,我将以下内容添加到 Maven Web 项目的 POM.XML 中。 com.MyCompany.sei MyCompany-a
我在 tomcat 上部署了 RESTful Web 服务'该 Web 服务在同一域场景的客户端/服务器中工作正常'但我需要使 Web 服务可从跨域使用'为此我使用了 CORS 过滤器. 我在 myP
我第一次尝试使用 ServletContextListener 在每次部署应用程序时执行特定函数。为此,我采用了一个简单的 java 类文件并在其上实现了 ServletContextListener
我是一名优秀的程序员,十分优秀!