[英]Taking up too much memory - python
我寫了一個遞歸函數,窮盡地生成了某些特征的矩陣。 功能如下:
def heavies(rowSums,colSums,colIndex,matH):
if colIndex == len(colSums) - 1:
for stuff in heavy_col_permutations(rowSums,colSums,colIndex):
matH[:,colIndex] = stuff[0]
yield matH.copy()
return
for stuff in heavy_col_permutations(rowSums,colSums,colIndex):
matH[:,colIndex] = stuff[0]
rowSums = stuff[1]
for matrix in heavies(rowSums,colSums,colIndex+1,matH):
yield matrix
和heavy_col_permutations是一個函數,它只返回一個具有我需要的特征的矩陣列。
問題是,當重量級產生大量矩陣時,它會占用太多內存。 我最終逐個從另一個函數調用它,最終我占用了太多RAM並且我的進程被殺死了(我在帶有內存上限的服務器上運行它)。 如何編寫它以減少內存使用?
該程序看起來像:
r = int(argv[1])
n = int(argv[2])
m = numpy.zeros((r,r),numpy.dtype=int32)
for row,col in heavy_listing(r,n):
for matrix in heavies(row,col,0,m):
# do more stuff with matrix
我知道重量函數是大量內存吸吮的地方,我只需要減少它。
你可以嘗試的事情:
heavies()
創建的矩陣副本不會在內存中保留引用。 gc
模塊,調用collect()
並使用set_threshold()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.