简体   繁体   English

防止我的RAM内存达到100%

[英]Prevent my RAM memory from reaching 100%

I have a very simple python script that reads a CSV file and sorts the rows according to the timestamps. 我有一个非常简单的python脚本,可读取CSV文件并根据时间戳对行进行排序。 However, the file is large enough (16 GB) that its reading uses ram memory completely. 但是,该文件足够大(16 GB),以至于其读取完全使用了内存。 When it reaches 100% (ie 64 GB RAM memory), my system completely freezes, and I am forced to restart my computer. 当达到100%(即64 GB RAM内存)时,我的系统完全死机,我被迫重新启动计算机。

Here is the code: 这是代码:

import pandas as pd
from time import time

filename = 'AKER_OB.csv'

start_ = time()
file_ = pd.read_csv(filename)
end_ = time()
duration = end_ - start_
print("The duration to load that file : {}".format(duration))

file_.to_datetime(df['TimeStamps'], format="%Y-%m-%d %H:%M:%S").sort_values()

Head of AKER_OB.csv : AKER_OB.csv负责人:

TimeStamp,Bid1,BidSize1,Bid2,BidSize2,Bid3,BidSize3,Bid4,BidSize4,Bid5,BidSize5,Bid6,BidSize6,Bid7,BidSize7,Bid8,BidSize8,Bid9,BidSize9,Bid10,BidSize10,Bid11,BidSize11,Bid12,BidSize12,Bid13,BidSize13,Bid14,BidSize14,Bid15,BidSize15,Bid16,BidSize16,Bid17,BidSize17,Bid18,BidSize18,Bid19,BidSize19,Bid20,BidSize20,Ask1,AskSize1,Ask2,AskSize2,Ask3,AskSize3,Ask4,AskSize4,Ask5,AskSize5,Ask6,AskSize6,Ask7,AskSize7,Ask8,AskSize8,Ask9,AskSize9,Ask10,AskSize10,Ask11,AskSize11,Ask12,AskSize12,Ask13,AskSize13,Ask14,AskSize14,Ask15,AskSize15,Ask16,AskSize16,Ask17,AskSize17,Ask18,AskSize18,Ask19,AskSize19,Ask20,AskSize20
2016-10-08 00:00:00,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
2016-10-08 00:00:07,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
2016-10-08 00:00:08,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

What is the correct way to fix this problem? 解决此问题的正确方法是什么? A full answer with code snippet will be appreciated. 带有代码段的完整答案将不胜感激。

Essentially, you have to implement your own out-of-memory sorting. 本质上,您必须实现自己的内存不足排序。

  1. Split your file in two or more pieces with Pandas CSV chunker , sort each piece (one piece at a time!), save it into a separate CSV file, and free the memory with del . 使用Pandas CSV块分割器将文件分为两段或更多段,将每一段排序(一次一件!),将其保存到单独的CSV文件中,然后使用del释放内存。

  2. Merge the sorted files by opening all of the saved pre-sorted files with CSV chunkers, combining the rows from the chunks, as needed, and appending the sorted rows to the output file. 通过使用CSV块工具打开所有已保存的预排序文件,合并合并的文件,并根据需要组合块中的行,并将已排序的行附加到输出文件中。

Just split the read of the file by chunks. 只需按块拆分读取的文件。 A similar case . 类似的情况

Also consider to add a swap partition or file to your OS, it will help the issue to be out of RAM for other situations. 还可以考虑将交换分区或文件添加到您的操作系统,这将有助于在其他情况下解决内存不足的问题。

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

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