用于大型数据格式化程序的Python垃圾回收器

我写了一个程序来读取一个excel文件的文件夹,并将每个文件加载到程序中。 然后获取数据并创build一个大小为零的数组(3001,001),这些数据将被迭代,并将来自excel的相应坐标值更改为1。 该arrays然后重新塑造(1,6005001)的大小。 我正在使用tensorflow来重新整形数组,因为程序认为它是一个元组,但是最终的值被存储在一个numpy数组中。 我最终将最终格式化的数组存储到名为“filename_Array.csv”的csv文件中,然后程序移动到下一个要格式化的excel文件。 我在Eclipse上运行Python并安装了tensorflow

我遇到的问题是有些值被caching在内存中,但我无法弄清楚它是什么。 我试过显式删除将被重新初始化的大型variables,并使用gc.collect()来清除存储的非活动内存。 我仍然看到内存使用率稳步增加,直到大约25个文件格式化,然后电脑开始冻结,因为我的电脑上的所有内存(12GB)正在使用。 我知道python会自动清除内存中的程序完全无法访问的值,所以我不确定这是RAM还是别的问题。 对不起,对文本的墙壁,我只是想尽可能多地给这个问题的信息。

这是一个链接到我的性能选项卡的屏幕截图时,通过约24个文件运行该程序之前,我不得不终止程序,由于计算机冻结。

这是我的代码:

from __future__ import print_function import os os.environ['TF_CPP_MIN_LOG_LEVEL']='2' import tensorflow as tf import numpy as np import csv import gc path = r'C:\Users\jeremy.desforges\Desktop\Eclipse\NN_MNIST\VAM SLIJ-II 4.500' def create_array(g,h,trainingdata,filename): # Multiplying by factors of 10 to keep precision of data g = g*1000 h = h*1 max_g = 3000 max_h = 2000 # Initializes an array with zeros to represent a blank graph image = np.zeros((max_g+1,max_h+1),dtype=np.int) shape = ((max_g+1)*(max_h+1)) # Fills the blank graph with the input data points for i in range(len(h)): image[g[i].astype('int'),h[i].astype('int')] = 1 trainingdata.close() image = tf.reshape(image,[-1,shape]) # Converts tensor objects to numpy arrays to feed into network sess = tf.InteractiveSession() image = sess.run(image) np.savetxt((filename + "_Array.csv"), np.flip(image,1).astype(int), fmt = '%i' ,delimiter=",") print(filename, "appended") print("size",image.shape) print(image,"= output array") del image,shape,g,h,filename,sess return # Initializing variables image = [] shape = 1 g = 1.0 h = 1.0 f = 1 specials = '.csv' folder = os.listdir(path) for filename in folder: trainingdata = open(filename, "r+") filename = str(filename.replace(specials, '')) data_read = csv.reader(trainingdata) for row in data_read: in1 = float(row[0]) in2 = float(row[1]) if (f==0): z_ = np.array([in1]) g = np.hstack((g,z_)) q = np.array([in2]) h = np.hstack((h,q)) if (f == 1): g = np.array([in1]) h = np.array([in2]) f = 0 create_array(g,h,trainingdata,filename) gc.collect() image = [] shape = 1 g = 1.0 h = 1.0 f = 1