r - Large SpatialPolygonsDataFrame causing memory leak -


i have program loads large spatialpolygonsdataframe object (1.4gb) memory performs analysis, attempts remove object. however, @ system memory using system command free shows object remains until r session reset. can reproduce memory leak using rworldmap , rworlxtra packages make large list of world maps, each spatialpolygonsdataframe, trying remove them:

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"))) # frees memory rm(list="these.maps.smaller") gc(reset=t) # fails free memory rm(list="these.maps.large") gc(reset=t) 

edit here output calling system2("free") after each stage.

restarting r session...  > library(sp) > library(rworldmap) ### welcome rworldmap ### 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 rworldmap ### 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 

has got idea why case , way might possible remove 1 of these large sp objects without having reset session?


r version 3.2.3 (2015-12-10) platform: x86_64-redhat-linux-gnu (64-bit) running under: scientific linux 7.2 (nitrogen)

you don't show output. see:

> 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 > # fails free 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 

which suggest removing these large maps releases memory; value equal obtained in fresh session after loading packages.


Comments