python - 用于 blender 的 python 脚本中的内存泄漏

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

我是 python 的新手(我通常使用 c++)。我编写了一个 python 脚本,使用光线跟踪算法将 Blender 对象转换为二进制掩码文件。

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

我不确定 open() 命令是否将打开的文件存储在 RAM 中(我猜是这样),但这只会占 256MB 的 RAM 使用量,因为生成的文件大小,也没有解释为什么 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 中的传递方式不同,这一定是问题的根源,因为我不太明白这是如何工作的。

最佳答案

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

您对那个对象做了 512^3 次。如果 ob.ray_cast 将一些数据存储在 ob 对象上,那么我可以看出这是一个问题。尝试更换

ob.ray_cast(orig,axis*10000.0)

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

关于python - 用于 blender 的 python 脚本中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17584187/

相关文章:

arrays - 如何更有效地在 Angular 应用程序中合并 REST 调用结果

c# - Silverlight Toolkit 图表组件中可能存在内存泄漏 - 求助!

python - PyQt5 - 如何在 QMainWindow 类中显示图像?

python - Selenium webdriver.get() 方法并不总是有效

python - 在 pandas 数据透视表中设置多级索引

python - 如何迭代类方法

python - 从2D numpy数组中删除特定行值的数组的快速方法

c++ - 有哪些工具可以帮助查找句柄的内存泄漏?

python - Django CSS背景图片不会改变

python - 三元运算符与 if-else 语句的性能