简体   繁体   English

运行 python 脚本时内存不足

[英]Running out of RAM while running python script

I am running the following script in a google colab cell:我在 google colab 单元中运行以下脚本:

fs = 256 #samples per second
import gc

#because of ram restriction, runs 20 datasets per time
dataset = []
length = len(detail['dataset']) #length is 123
for i in range(length):
    name = detail['dataset'][i]
    start = detail['seizure start'][i] * 256
    end = detail['seizure end'][i] * 256
    f = pyedflib.EdfReader(name)
    n = f.signals_in_file
    signal_labels = f.getSignalLabels()
    sigbufs = np.zeros((n, f.getNSamples()[0]))
    for j in np.arange(n):
            sigbufs[j, :] = f.readSignal(j)
    l = sigbufs.shape[-1]
    t = np.linspace(0,l/fs,l)
    f.close()
    start = start - 100
    end = end + 100
    dataset.append([t[start:end], sigbufs[:,start:end]])
    print("completed run " + str(i) + " out of " + str(length))
    del sigbufs
    del f
    gc.collect()

import pickle
with open("dataset.txt",'wb') as fp:
    pickle.dump(dataset, fp)

Intuitively, I thought that every time the cycle happens, the program deletes the previous values for sigbufs and f , the data and the object containing the data respectively.直觉上,我认为每次循环发生时,程序都会分别删除sigbufsf的先前值、数据和包含数据的对象。 Apparently this was not the case as the RAM in google colab crashed, thus adding the del sigbufs and del f at the end but this didn't work either.显然情况并非如此,因为 google colab 中的 RAM 崩溃了,因此在最后添加了del sigbufsdel f但这也不起作用。

Is there a way to clear the ram and not make it crash?有没有办法清除内存而不是让它崩溃? if I do it manually, say if I do 20 datasets at a time the RAM does not crash (because it can handle that amount).如果我手动执行,假设我一次执行 20 个数据集,RAM 不会崩溃(因为它可以处理该数量)。

NOTE : The finalized bit that I want to save is not that large, it's the actual dataset that is relatively large.注意:我要保存的最终确定的位不是那么大,它是相对较大的实际数据集。

I have also faced similar problem in one of my machine learning training codes that works with multiple datasets in a loop.在我的一个机器学习训练代码中,我也遇到了类似的问题,该代码循环处理多个数据集。 Python's garbage collector is pretty lame in the case that you are facing.在您面临的情况下,Python 的垃圾收集器非常蹩脚。 I would suggest you to use another python file (script file) to run your current python file (main file) without the loop in the current main file.我建议你使用另一个 python 文件(脚本文件)来运行你当前的 python 文件(主文件),而不是当前主文件中的循环。 You will provide the loop in the new script file and also merge everything in that script file.您将在新脚本文件中提供循环,并合并该脚本文件中的所有内容。 In that way, iterations that you are performing in your current main file will be performed as separate runs via the script file and your problem will be eliminated.这样,您在当前主文件中执行的迭代将通过脚本文件作为单独的运行执行,您的问题将被消除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM