簡體   English   中英

連接或vstack numpy數組使內存加倍

[英]concatenate or vstack numpy arrays doubling memory

以前的一些信息

我有以下方法,該方法從磁盤加載兩個數據集,然后將它們組合並返回組合的數據集。

def _get_data(self, data_set_name):
    training_data = DataSet.from_file('path_to_data_file','path_to_label_file')
    test_data = DataSet.from_file('path_to_data_file','path_to_label_file')
    return training_data.concat(test_data) # doubles the memory consumption

DataSet是這樣的:

class DataSet:
  def __init__(self, data, labels):
    self.x = data # float64 array of shape (x,y)
    self.y = labels # int array of shape (x,)

  def concat(self, other_data_set):
    new_x = numpy.vstack((self.x, other_data_set.x))
    new_y = numpy.concatenate((self.y, other_data_set.y))

    return DataSet(new_x, new_y)

我的問題

調用DataSet.concat ,內存將增加一倍。 首先,這是預期的行為,因為numpy創建了兩個包含合並數據的新數組。 但是 ,離開_get_data方法后,變量training_datatest_data應該不再引用較小的數據集。 因此,我希望內存消耗應該再次減少。 這不會發生。 因為我在想,我嘗試手動調用垃圾回收,但沒有成功。

data_set = _get_data('someName')
gc.collect(0)
gc.collect(1)
gc.collect(2)
# Still same memory consumption

誰能告訴我這里發生了什么? 難道我做錯了什么?

更新資料

我通過以下代碼測量內存:

pid = os.getpid()
py = psutil.Process(pid)
memoryUse = py.memory_info()[0] / 2. ** 30  # memory use in GB
print('memory use:', memoryUse)

似乎是由於調試而發生此行為。 當我在_get_data之前放置斷點時,退出_get_data並繼續前進后,內存沒有釋放。 當我正確釋放對_get_data內存的調用后放置第一個斷點時。 我不知道調試可能會對內存管理產生如此大的影響。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM