【问题标题】:Releasing memory usage by variable in python在python中通过变量释放内存使用量
【发布时间】:2017-09-07 00:14:53
【问题描述】:

我目前正在尝试将一些数据存储到 .h5 文件中,我很快意识到可能必须将我的数据存储到部分中,因为无法在我的 ram 中处理它。我开始使用numpy.array 来压缩内存使用量,但这导致在格式化数据上花费了几天的时间。

所以我回去使用list,但让程序监控内存使用情况, 当它高于指定值时,是否将部分存储为numpy 格式 - 以便另一个进程可以加载并使用它。这样做的问题是,我认为会释放我的内存的东西并没有释放内存。出于某种原因,即使我重置了变量和del 变量,内存也是一样的。为什么这里没有释放内存?

import numpy as np
import os
import resource
import sys
import gc
import math
import h5py
import SecureString
import objgraph
from numpy.lib.stride_tricks import as_strided as ast

total_frames = 15
total_frames_with_deltas = total_frames*3
dim = 40
window_height = 5


def store_file(file_name,data):
    with h5py.File(file_name,'w') as f:
        f["train_input"] = np.concatenate(data,axis=1)

def load_data_overlap(saved):
    #os.chdir(numpy_train)
    print "Inside function!..."
    if saved == False:
        train_files = np.random.randint(255,size=(1,40,690,4))
        train_input_data_interweawed_normalized = []
        print "Storing train pic to numpy"
        part = 0
        for i in xrange(100000):
            print resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
            if resource.getrusage(resource.RUSAGE_SELF).ru_maxrss > 2298842112/10:
                print "Max ram storing part: " + str(part) + " At entry: " + str(i)
                print "Storing Train input"
                file_name = 'train_input_'+'part_'+str(part)+'_'+str(dim)+'_'+str(total_frames_with_deltas)+'_window_height_'+str(window_height)+'.h5'
                store_file(file_name,train_input_data_interweawed_normalized)
                part = part + 1             
                del train_input_data_interweawed_normalized
                gc.collect()
                train_input_data_interweawed_normalized = []
                raw_input("something")
            for plot in train_files:
                overlaps_reshaped = np.random.randint(10,size=(45,200,5,3))
                for ind_plot in overlaps_reshaped.reshape(overlaps_reshaped.shape[1],overlaps_reshaped.shape[0],overlaps_reshaped.shape[2],overlaps_reshaped.shape[3]): 
                    ind_plot_reshaped = ind_plot.reshape(ind_plot.shape[0],1,ind_plot.shape[1],ind_plot.shape[2])
                    train_input_data_interweawed_normalized.append(ind_plot_reshaped)
    print len(train_input_data_interweawed_normalized)

    return train_input_data_interweawed_normalized_print
#------------------------------------------------------------------------------------------------------------------------------------------------------------

saved = False
train_input = load_data_overlap(saved)

输出:

.....
223662080
224772096
225882112
226996224
228106240
229216256
230326272
Max ram storing part: 0 At entry: 135
Storing Train input
something
377118720
Max ram storing part: 1 At entry: 136
Storing Train input
something
377118720
Max ram storing part: 2 At entry: 137
Storing Train input
something

【问题讨论】:

  • 在我看来,这对于MCVE 来说有点笨重。您的问题与通过将长列表保存到文件然后删除列表来释放 for 循环内的内存有关。尝试剪切 for plot in train_files: 循环内的所有内容,然后将 random variables 列表添加到循环中每个点的列表中。然后提供一些输出。
  • 添加新版本和输出
  • 您已删除 del 语句。此外,与其将整个数组保存到一个新变量中(并在此过程中占用两倍的内存),不如先保存它然后删除它:h5f.create_dataset('train_input', data=np.concatenate(train_input_data_interweawed_normalized,axis=1))
  • 你的意思是这样的? :pastebin.com/25MtFeii
  • 是的,但没有这一切:train_input_data_interweawed_normalized = None train_input_data_interweawed_normalized = [] del h5f。更新后的输出是什么?

标签: python macos list numpy memory


【解决方案1】:

你需要显式强制垃圾回收,见here

根据Python官方文档,可以通过gc.collect()强制垃圾回收器释放未引用的内存

【讨论】:

  • 好的,你是在每个del 之后的循环内做的吗?您能否提供(在问题文本中)程序运行时的内存使用情况?或者,如果你感觉很慷慨。 MCVE?
  • 上面给出的示例是一个最小的完整工作示例 - 随机生成的数据 - 我正在执行的实际过程以及存储。您应该能够按原样运行代码。我在 raw_input("Something!") 之前使用了垃圾收集器,然后看到内存使用量比上一个值增加了。我将添加代码的输出。
  • 查看问题评论
猜你喜欢
  • 2017-12-26
  • 1970-01-01
  • 1970-01-01
  • 2019-10-12
  • 1970-01-01
  • 1970-01-01
  • 2016-07-20
  • 2011-02-10
  • 1970-01-01
相关资源
最近更新 更多