Repository navigation
fread: memory not being freed #3292
Copy link
Copy link
Closed
Description
Activity
For a comparison, I tried the readr:read_csv function. This seems to free up most of the memory.
read <- readr::read_csv("/mnt/batch/tasks/shared/fileshare/mdo_data/downloads/TREstSum2000"); rm(read); gc()top command output:
PID USER PR NI VIRT RES SHR S %CUP %MEM TIME + COMMAND 10512 patrick+ 20 0 465.4m 139.0m 25.5m S 0.0 0.1 0:36.46 rsessionIf you run the read.csv command from base R, the same problem as fread.
#3719842 33 read <- read.csv("/mnt/batch/tasks/shared/fileshare/mdo_data/downloads/TREstSum2000"); rm(read); gc()top command results:
PID USER PR NI VIRT RES SHR S %CUP %MEM TIME + COMMAND 12169 patrick+ 20 0 1879.1m 1.518g 24.6m S 0.3 1.4 1:44.99 rsessionJust came across this post while solving memory leakage issue. One of the problems is still with fread.
require(data.table) for(i in 1:10) { sampleDt <- fread("sample-data.csv", header = TRUE, stringsAsFactors = FALSE) rm(sampleDt) gc() print(pryr::mem_used()) } # 54.3 MB # 61.4 MB # 68.3 MB # 75.2 MB # 82.2 MB # 89.1 MB # 96 MB # 103 MB # 110 MB # 117 MBUsing read.csv has no leakage.
for(i in 1:10) { sampleDt <- read.csv("sample-data.csv", header = TRUE, stringsAsFactors = FALSE) rm(sampleDt) gc() print(pryr::mem_used()) } # 47.7 MB # 47.8 MB # 47.8 MB # 47.8 MB # 47.8 MB # 47.8 MB # 47.8 MB # 47.8 MB # 47.8 MB # 47.8 MBsessionInfo()
R version 3.6.0 (2019-04-26) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 17763) Matrix products: default locale: [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] data.table_1.12.2 loaded via a namespace (and not attached): [1] compiler_3.6.0 pryr_0.1.4 magrittr_1.5 tools_3.6.0 Rcpp_1.0.1 stringi_1.4.3 codetools_0.2-16 stringr_1.4.0Confirming reproducibly on Mac:
library(data.table) tmp = tempfile() iris = as.data.table(iris) DT = rbindlist(replicate(1e5, iris, simplify = FALSE)) fwrite(DT, tmp) for(i in 1:10) { sampleDt <- fread(tmp, header = TRUE, stringsAsFactors = FALSE) rm(sampleDt) gc() print(pryr::mem_used()) } 736 MB 875 MB 1.01 GB 1.15 GB 1.29 GB 1.43 GB 1.57 GB 1.71 GB 1.85 GB 1.98 GBFinishing my 6 hour memory leak search here. Sadly, same issue here with fread. Running MacOS with 1.12.8 data.table.
My memory usage progression looks like
33 MB
283 MB
533 MB
783 MB
1.03 GB
1.28 GB
1.53 GB
1.78 GB
2.03 GB
2.28 GBReacted by Jordan Rule and Hause Lin- added a commit that references this issue
on Sep 18, 2020 - added a commit that references this issue
on Jan 25, 2024 - added a commit that references this issue
on May 11, 2026 - added a commit that references this issue
on May 14, 2026
I have found that reading data into a data.table will cause a memory leak. This seems to happen whether I use fread or read_fst(from the fst package). I suspect that this could even be an R or OS issue, however, I have not found any cases of this on Stack Overflow...
Steps to reproduce:
--If you want to run in it with valgrind, you can use the following:
4. Run the following valgrind command (change the source script to the location of your R script from step 2):
Here is the end of the valgrind output.
5.
sessionInfo()