Numpy和matplotlib垃圾收集

Pro*_*ala 6 python numpy matplotlib

我有一个python脚本,它为不同的参数(Q,K)进行了许多模拟,绘制结果并将其存储到磁盘.

每组参数(Q,K)产生一个200x200x80数据点的3D体积网格数据,这需要~100 MB的数据.然后,该体积网格的一部分逐层绘制,产生~60个图像.

问题是python显然不会在此过程中释放内存.我不确定内存泄漏在哪里,或者规则如何控制python如何决定哪些对象被释放.我也不确定numpy数组或matplotlib图形对象中的内存是否丢失.

  1. 有没有一种简单的方法来分析 python中的哪些对象在内存中持久存在以及哪些对象被自动释放?
  2. 没有办法强制python释放所有数组并计算在特定循环周期或特定函数调用中创建的对象

代码的相关部分在这里(但是,它不会运行...模拟代码的大部分包括ctypesC++/python接口被省略,因为它太复杂了):

import numpy as np
import matplotlib.pyplot as plt
import ProbeParticle as PP # this is my C++/Python simulation library, take it as blackbox

def relaxedScan3D( xTips, yTips, zTips ):
    ntips = len(zTips); 
    print " zTips : ",zTips
    rTips = np.zeros((ntips,3)) # is this array deallocated when exiting the function?
    rs    = np.zeros((ntips,3)) # and this?
    fs    = np.zeros((ntips,3)) # and this?
    rTips[:,0] = 1.0
    rTips[:,1] = 1.0
    rTips[:,2] = zTips 
    fzs    = np.zeros(( len(zTips), len(yTips ), len(xTips ) )); # and this?
    for ix,x in enumerate( xTips  ):
        print "relax ix:", ix
        rTips[:,0] = x
        for iy,y in enumerate( yTips  ):
            rTips[:,1] = y
            itrav = PP.relaxTipStroke( rTips, rs, fs ) / float( len(zTips) )
            fzs[:,iy,ix] = fs[:,2].copy()
    return fzs


def plotImages( prefix, F, slices ):
    for ii,i in enumerate(slices):
        print " plotting ", i
        plt.figure( figsize=( 10,10 ) ) # Is this figure deallocated when exiting the function ?
        plt.imshow( F[i], origin='image', interpolation=PP.params['imageInterpolation'], cmap=PP.params['colorscale'], extent=extent )
        z = zTips[i] - PP.params['moleculeShift' ][2]
        plt.colorbar();
        plt.xlabel(r' Tip_x $\AA$')
        plt.ylabel(r' Tip_y $\AA$')
        plt.title( r"Tip_z = %2.2f $\AA$" %z  )
        plt.savefig( prefix+'_%3.3i.png' %i, bbox_inches='tight' )

Ks = [ 0.125, 0.25, 0.5, 1.0 ]
Qs = [ -0.4, -0.3, -0.2, -0.1, 0.0, +0.1, +0.2, +0.3, +0.4 ]

for iq,Q in enumerate( Qs ):
    FF = FFLJ + FFel * Q
    PP.setFF_Pointer( FF )
    for ik,K in enumerate( Ks ):
        dirname = "Q%1.2fK%1.2f" %(Q,K)
        os.makedirs( dirname )
        PP.setTip( kSpring = np.array((K,K,0.0))/-PP.eVA_Nm )
        fzs = relaxedScan3D( xTips, yTips, zTips ) # is memory of "fzs" recycled or does it consume more memory each cycle of the loop ?
        PP.saveXSF( dirname+'/OutFz.xsf', headScan, lvecScan, fzs )
        dfs = PP.Fz2df( fzs, dz = dz, k0 = PP.params['kCantilever'], f0=PP.params['f0Cantilever'], n=int(PP.params['Amplitude']/dz) ) # is memory of "dfs" recycled?
        plotImages( dirname+"/df", dfs, slices = range( 0, len(dfs) ) )
Run Code Online (Sandbox Code Playgroud)

til*_*ten 8

尝试重用你的数字:

plt.figure(0, figsize=(10, 10))
plt.clf() #clears figure
Run Code Online (Sandbox Code Playgroud)

保存后关闭您的数字:

...
plt.savefig(...)
plt.close()
Run Code Online (Sandbox Code Playgroud)

  • 啊哈,谢谢,看来`plt.close()` 没有内存泄漏了。尽管如此,最好清楚地了解规则是什么以及如何分析这种泄漏。 (2认同)