【问题标题】:memory leak in python script for blender搅拌机的python脚本中的内存泄漏
【发布时间】:2013-07-09 04:22:42
【问题描述】:

我对 python 还很陌生(我通常使用 c++)。我编写了一个 python 脚本,使用光线追踪算法将 Blender 对象转换为二进制蒙版文件。

我最终会出现巨大的内存泄漏。对于 M = 512(如下所示),脚本在运行时会增加 RAM 使用量,最终结果是使用了高达 5.7GB 的内存。在脚本运行后,这种 RAM 使用量也会保留,直到我关闭 Blender。

我不确定open() 命令是否将打开的文件存储在 RAM 中(我猜是这样),但这只会占用 256MB 的 RAM 使用量,因为这是生成的文件大小,而且确实如此不解释为什么在fo.close() 之后内存使用仍然存在。

我确定我缺少一些非常简单的东西,但对 python 不是很熟悉,我很难弄清楚它是什么。我尝试通过放置行来清除pointInsideMesh() 函数中使用的所有变量

axis  = None
mat1  = None
mat   = None
orig  = None
count = None
location = None
normal   = None
index    = None
outside1 = None
outside2 = None

紧接在return 语句之前,但这并没有堵塞内存泄漏。这是我的代码:

import mathutils, numpy, bpy





def pointInsideMesh(point,ob):

    axes = [ mathutils.Vector((1,0,0)) ]
    outside1 = False
    for axis in axes:

        mat1 = mathutils.Matrix(ob.matrix_world)
        mat=mat1.invert()

        orig = mat1*point


        count = 0
        while True:
            location,normal,index = ob.ray_cast(orig,axis*10000.0)
            if index == -1: break
            count+= 1

            orig = location + axis*0.00001



        if (count%2 == 0):
            outside1 = True
            break




    axes = [ mathutils.Vector((0,1,0)) ]
    outside2 = False
    for axis in axes:

        mat1 = mathutils.Matrix(ob.matrix_world)
        mat=mat1.invert()

        orig = mat1*point


        count = 0
        while True:
            location,normal,index = ob.ray_cast(orig,axis*10000.0)
            if index == -1: break
            count+= 1

            orig = location + axis*0.00001



        if (count%2 == 0):
            outside2 = True
            break

    outside = outside1 or outside2
    return not outside




ob = bpy.context.active_object
M = 512
fileOut = 'D:\images\\maskFile.txt'

fo = open(fileOut , "w")
for n in range(0,M):
    for m in range(0,M):
        for l in range(0,M):
            if pointInsideMesh( mathutils.Vector(((l+1)/M-0.5,(m+1)/M-0.5,(n+1)/M-0.5)), ob ):
                fo.write("1")
            else:
                fo.write("0")
            if l < M-1:
                fo.write(" ")
        fo.write("\n") 
    fo.write("\n")          
fo.close()

任何帮助将不胜感激:)

更新:

import mathutils, numpy, bpy

def pointInsideMesh(point,ob):
    return False

ob = bpy.context.active_object
M = 512
fileOut = 'D:\images\\maskFile.txt'

fo = open(fileOut , "w")
for n in range(0,M):
    for m in range(0,M):
        for l in range(0,M):
            if pointInsideMesh( mathutils.Vector(((l+1)/M-0.5,(m+1)/M-0.5,(n+1)/M-0.5)), ob ):
                fo.write("1")
            else:
                fo.write("0")
            if l < M-1:
                fo.write(" ")
        fo.write("\n") 
    fo.write("\n")          
fo.close()

重现问题,但是

import mathutils, numpy, bpy

def pointInsideMesh():
    return False

ob = bpy.context.active_object
M = 512
fileOut = 'D:\images\\maskFile.txt'

fo = open(fileOut , "w")
for n in range(0,M):
    for m in range(0,M):
        for l in range(0,M):
            if pointInsideMesh():
                fo.write("1")
            else:
                fo.write("0")
            if l < M-1:
                fo.write(" ")
        fo.write("\n") 
    fo.write("\n")          
fo.close()

没有,唯一的区别是在这两个示例中的第一个示例中,被测试点(和对象)的位置被传递给函数,而在第二个示例中,没有传递任何参数。在 C++ 中,即使按值传递,在函数内创建的参数副本也会在函数返回时从内存中清除。但是在python中参数的传递方式不同,这一定是问题的根源,因为我不太明白这是如何工作的。

【问题讨论】:

    标签: python memory-leaks python-3.x blender


    【解决方案1】:

    我能看到的唯一会导致内存泄漏的是ob 对象。

    你对那个对象做了512^3次。如果ob.ray_castob 对象上存储一些数据,那么我可以看到这是一个问题。换个试试

    ob.ray_cast(orig,axis*10000.0)
    

    使用 3 个静态值并查看内存问题是否仍然存在。 Blender 的 C 端可能存在内存泄漏。

    【讨论】:

    • 感谢您的评论。我首先尝试了这个,但没有成功,但我已经做出了更重大的改变,试图查明发生泄漏的位置。这可以在我原来的问题的更新中看到。再次感谢:)
    猜你喜欢
    • 1970-01-01
    • 2012-02-07
    • 2014-04-09
    • 2020-01-06
    • 2014-01-15
    • 1970-01-01
    • 2013-02-14
    • 2017-06-15
    • 2019-12-22
    相关资源
    最近更新 更多