最終更新:2018/11/26 はじめに pythonはLLですが、なぜかメモリを何十GBも消費するような(一般的なPCのリソースからすれば)大規模なデータ分析に広く使われています。このようなデータ分析では、往々にしてメモリ不足が生じ、それなりに配慮してプログラムを書かないとそもそもプログラムが走らない、MemoryErrorが出るといった事態が発生しがちです。 そういうときにやるべきことをつらつらと書いていきます。なお、下の方に行くほど邪悪度()が増していきます。 目次 はじめに 対策 メモリを増設する・システムのswap領域を増やす multiprocessingを使っているなら使うのをやめる、あるいはプロセス数を減らす 要らないデータはGCに回収させる リストは積極的にnumpy配列にする 疎行列型配列を使用する 32bitにする 配列処理は破壊的代入で行う numpyの機能に頼る