首页 > 解决方案 > PBS vmem 超出限制:我怎么知道内存超出了哪里?

问题描述

我有一个包含 1500000 个多边形的 shapefile,我需要转到每个多边形并将其与不同的网格相交。

我创建了一个简单的程序,从多边形到多边形的交叉点(使用多处理),

pool = mp.Pool() 
for index,pol in shapefile.iterrows():
        # Limits each polygon in shapefile     
        ylat = lat_gridlimits
        xlon= lon_gridlimits
        args.append((dgrid,ylat,xlon,pol,index))      
pool.starmap(calculate,args) 
pool.close() 
pool.join()

但是内存很快就会填满,我得到一个错误

PBS:作业终止:vmem 超出限制

我如何知道内存超出的位置或时间?或者有没有办法控制每个函数中的内存?

我试过这个(内部计算):

process = psutil.Process(os.getpid())
mem=process.memory_info().rss/(1024.0 ** 3)
vmem=psutil.virtual_memory().total / (1024.0 ** 3)
print("{}  {}\n".format(mem,vmem)) 

但这并不能帮助我找到在哪里

标签: pythonmemorymultiprocessingshapefilepsutil

解决方案


内存不足的一个原因可能是因为您在 for 循环中使用迭代器来迭代非常大的数据集。迭代这个集合可能需要比 python 程序允许在你的系统上使用更多的内存。节省内存的一种方法是将作为迭代器的 shapefile.iterrows() 重写为返回生成器的函数,因为生成器计算需要读取的新索引,而不是存储所有索引。

要阅读有关生成器的更多信息,请访问以下链接:

https://pythongeeks.org/python-generators-with-examples/


推荐阅读