2016-07-21 7 views
1

メモリに大量のSpatialPolygonsDataFrameオブジェクト(1.4Gb)をロードして解析を実行し、オブジェクトを削除しようとするプログラムがあります。ただし、システムコマンドfreeを使用してシステムメモリを見ると、Rセッションがリセットされるまでオブジェクトが残っていることがわかります。 私はSpatialPolygonsDataFrameそれぞれ、世界地図の大規模なリストを作成するrworldmaprworlxtraパッケージを使用してメモリリークを再現することができ、その後、それらを削除しようとしている:メモリリークの原因となる大きなSpatialPolygonsDataFrame

install.packages("sp") 
install.packages("rworldmap") 
install.packages("rworldxtra") 
library(sp) 
library(rworldmap) 
library(rworldxtra) 

these.maps.large <- lapply(1:100, function(x) assign(paste0("a_", x), getMap(resolution = "high"))) 
these.maps.smaller <- lapply(1:20, function(x) assign(paste0("a_", x), getMap(resolution = "high"))) 
# This frees the memory 
rm(list="these.maps.smaller") 
gc(reset=T) 
# This fails to free the memory 
rm(list="these.maps.large") 
gc(reset=T) 

EDIT ここでは、システム2を呼び出すための出力であります(「無料」)。

Restarting R session... 

> library(sp) 
> library(rworldmap) 
### Welcome to rworldmap ### 
For a short introduction type :  vignette('rworldmap') 
> library(rworldxtra) 
> system2("free") 
       total  used  free  shared buff/cache available 
Mem:  131987656  1386468 118712292  540008 11888896 129731040 
Swap:  4194300  3505464  688836 
> these.maps.large <- lapply(1:100, function(x) assign(paste0("a_", x), getMap(resolution = "high"))) 
> system2("free") 
       total  used  free  shared buff/cache available 
Mem:  131987656  2708040 117390660  540008 11888956 128409404 
Swap:  4194300  3505464  688836 
> rm(list="these.maps.large") 
> gc(reset=T) 
     used (Mb) gc trigger (Mb) max used (Mb) 
Ncells 585803 31.3 9601876 512.8 585803 31.3 
Vcells 711198 5.5 96623732 737.2 711198 5.5 
> system2("free") 
       total  used  free  shared buff/cache available 
Mem:  131987656  2708428 117390424  540008 11888804 128409168 
Swap:  4194300  3505464  688836 

Restarting R session... 

> library(sp) 
> library(rworldmap) 
### Welcome to rworldmap ### 
For a short introduction type :  vignette('rworldmap') 
> library(rworldxtra) 
> system2("free") 
       total  used  free  shared buff/cache available 
Mem:  131987656  1386696 118711988  540008 11888972 129730744 
Swap:  4194300  3505464  688836 
> these.maps.smaller <- lapply(1:20, function(x) assign(paste0("a_", x), getMap(resolution = "high"))) 
> system2("free") 
       total  used  free  shared buff/cache available 
Mem:  131987656  1699628 118399100  540008 11888928 129417836 
Swap:  4194300  3505464  688836 
> rm(list="these.maps.smaller") 
> gc(reset=T) 
     used (Mb) gc trigger (Mb) max used (Mb) 
Ncells 702817 37.6 2564361 137.0 702817 37.6 
Vcells 966452 7.4 21638748 165.1 966452 7.4 
> system2("free") 
       total  used  free  shared buff/cache available 
Mem:  131987656  1699612 118399116  540008 11888928 129417852 
Swap:  4194300  3505464  688836 

は、誰もがこのような場合、セッションをリセットすることなく、これらの大規模なSPのオブジェクトの1つを除去することができるかもしれませんどのような方法である理由を任意のアイデアを持っていますか?


Rバージョン3.2.3(2015年12月10日) プラットフォーム:x86_64版-redhatの-のlinux-gnuの(64ビット) の下で実行:科学のLinux 7.2(窒素)

答えて

2

あなたドン出力を表示しないでください。私は参照してください:

> rm(list="these.maps.smaller") 
> gc(reset=T) 
      used (Mb) gc trigger (Mb) max used (Mb) 
Ncells 7782803 415.7 14442815 771.4 7782803 415.7 
Vcells 113371012 865.0 184235296 1405.7 113371012 865.0 
> # This fails to free the memory 
> rm(list="these.maps.large") 
> gc(reset=T) 
     used (Mb) gc trigger (Mb) max used (Mb) 
Ncells 524121 28 11554252 617.1 524121 28 
Vcells 649283 5 147388236 1124.5 649283 5 

これらの大きな地図を削除すると、ほとんどのメモリが解放されることがわかります。その値は、パッケージをロードした後の新しいセッションで得られた値とほぼ同じです。

関連する問題