[R] Problem retrieving memory used after gc() using arrow library
Attachments :
r_code.txt
r_code_with_output.txt
Hello,
I'm using R versions 4.1.3 and 4.2.1 on Windows and I'm having a problem with memory usage.
Currently, I need to use the arrow and dplyr libraries in a program and when I compare the memory used between the windows task manager and the memory.size(max=F) function, the one given by the windows task manager is much larger, 243.5 MB, than the one given by the memory.size(max=F) function, 75.77 MB.
However, I delete objects created with rm() and then use the gc() function to recover the memory used by the object.
Below, with and without ouput, the R code I used to present my problem :
Do you think this memory difference is normal? Could it be caused by the libraries used and/or by bad practices in using the R language?
Thank you for your help, and I remain at your disposal for any further information you may require.
Best regards,
Component(s)
R
The Windows Task Manager and memory.size() and gc() all get their numbers from different places, so I'm not surprised that there are differences (although I'm not familiar with the details on Windows). I do know that any allocations made by Arrow C++ won't show up in gc(); however you can track these allocations using default_memory_pool()$bytes_allocated. Note that there are some hidden references to objects that are not always apparent (for example, when converting a Table to a data.frame, some columns may be zero-copy shells around Arrow arrays).
library(arrow, warn.conflicts = FALSE)
default_memory_pool()$bytes_allocated
#> [1] 0
default_memory_pool()$max_memory
#> [1] 0
# no bytes allocated because it has re-used R's memory
array <- as_arrow_array(1:10)
default_memory_pool()$bytes_allocated
#> [1] 0
default_memory_pool()$max_memory
#> [1] 0
# Can't re-use R memory for decimal type, so this will trigger an Arrow allocation
array <- as_arrow_array(1:10, type = decimal(10, 3))
default_memory_pool()$bytes_allocated
#> [1] 192
default_memory_pool()$max_memory
#> [1] 256
rm(array)
gc()
#> used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
#> Ncells 803037 42.9 1418702 75.8 NA 1418702 75.8
#> Vcells 1370077 10.5 8388608 64.0 16384 2707166 20.7
default_memory_pool()$bytes_allocated
#> [1] 0
default_memory_pool()$max_memory
#> [1] 256
Created on 2023-06-19 with reprex v2.0.2
Hello paleolimbot,
Thank you for your reply.
Compared to the results I get by adding the functions you advised, I don't quite understand what exactly the functions default_memory_pool()$bytes_allocated and default_memory_pool()$max_memory do.
Below is the code with the outputs I ran :
> gc(verbose = TRUE)
Garbage collection 2 = 0+0+2 (level 2) ...
14.2 Mbytes of cons cells used (41%)
3.9 Mbytes of vectors used (6%)
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 264908 14.2 648748 34.7 401965 21.5
Vcells 500529 3.9 8388608 64.0 1671274 12.8
>
> # basic memory
> memory.size(max=F)
[1] 28.78
>
> library(arrow, warn.conflicts = FALSE)
>
> # Memory after loading the arrow library with memory.size
> memory.size(max=F)
[1] 51.01
>
> # bytes_allocated after loading the arrow library
> default_memory_pool()$bytes_allocated
[1] 0
>
> # max_memory after loading the arrow library
> default_memory_pool()$max_memory
[1] 0
>
> library(dplyr)
Attachement du package : ‘dplyr’
Les objets suivants sont masqués depuis ‘package:stats’:
filter, lag
Les objets suivants sont masqués depuis ‘package:base’:
intersect, setdiff, setequal, union
>
> # Memory after loading the dplyr library with memory.size
> memory.size(max=F)
[1] 90.74
>
> # bytes_allocated after loading the dplyr library
> default_memory_pool()$bytes_allocated
[1] 0
>
> # max_memory after loading the dplyr library
> default_memory_pool()$max_memory
[1] 0
>
> df <- data.frame(
+ col1 = rnorm(1000000),
+ col2 = rnorm(1000000),
+ col3 = runif(1000000),
+ col4 = sample(1:999, size = 1000000, replace = T),
+ col5 = sample(c("GroupA", "GroupB"), size = 1000000, replace = T),
+ col6 = sample(c("TypeA", "TypeB"), size = 1000000, replace = T)
+ )
>
> # Memory after df object creation
> memory.size(max=F)
[1] 133.23
>
> # bytes_allocated after df object creation
> default_memory_pool()$bytes_allocated
[1] 0
>
> # max_memory after df object creation
> default_memory_pool()$max_memory
[1] 0
>
> arrow::write_dataset(
+ df,
+ paste0(Sys.getenv("USERPROFILE"),"/ExProblemeGc"),
+ format = "parquet"
+ )
>
> # Memory after writing to disk
> memory.size(max=F)
[1] 120.07
>
> # bytes_allocated after writing to disk
> default_memory_pool()$bytes_allocated
[1] 19000128
>
> # max_memory after writing to disk
> default_memory_pool()$max_memory
[1] 27126592
>
> rm(df)
>
> # Memory after deletion df
> memory.size(max=F)
[1] 120.07
>
> # bytes_allocated after deletion df
> default_memory_pool()$bytes_allocated
[1] 19000128
>
> # max_memory after deletion df
> default_memory_pool()$max_memory
[1] 27126592
>
> gc(verbose = TRUE)
Garbage collection 15 = 9+2+4 (level 2) ...
45.0 Mbytes of cons cells used (61%)
38.0 Mbytes of vectors used (49%)
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 842008 45 1387691 74.2 1387691 74.2
Vcells 4975717 38 10146329 77.5 8388601 64.0
>
> # Memory after gc(verbose = TRUE)
> memory.size(max=F)
[1] 101.29
>
> # bytes_allocated after gc(verbose = TRUE)
> default_memory_pool()$bytes_allocated
[1] 0
>
> # max_memory after gc(verbose = TRUE)
> default_memory_pool()$max_memory
[1] 27126592
>
> gc(verbose = TRUE)
Garbage collection 16 = 9+2+5 (level 2) ...
45.0 Mbytes of cons cells used (61%)
11.3 Mbytes of vectors used (15%)
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 841895 45.0 1387691 74.2 1387691 74.2
Vcells 1475542 11.3 10146329 77.5 8388601 64.0
>
> # Memory after gc(verbose = TRUE)
> memory.size(max=F)
[1] 74.35
>
> # bytes_allocated after gc(verbose = TRUE)
> default_memory_pool()$bytes_allocated
[1] 0
>
> # max_memory after gc(verbose = TRUE)
> default_memory_pool()$max_memory
[1] 27126592
>
> ds <- arrow::open_dataset(paste0(Sys.getenv("USERPROFILE"),"/ExProblemeGc"))
>
> # Memory after ds creation
> memory.size(max=F)
[1] 79.01
>
> # bytes_allocated after ds creation
> default_memory_pool()$bytes_allocated
[1] 0
>
> # max_memory after ds creation
> default_memory_pool()$max_memory
[1] 27126592
>
> req <-
+ ds %>%
+ collect()
>
> # Memory after req creation
> memory.size(max=F)
[1] 84.46
>
> # bytes_allocated after req creation
> default_memory_pool()$bytes_allocated
[1] 47504192
>
> # max_memory after req creation
> default_memory_pool()$max_memory
[1] 83176320
>
> rm(req)
>
> # Memory after deletion req
> memory.size(max=F)
[1] 84.47
>
> # bytes_allocated after deletion req
> default_memory_pool()$bytes_allocated
[1] 47504192
>
> # max_memory after deletion req
> default_memory_pool()$max_memory
[1] 83176320
>
> gc(verbose = TRUE)
Garbage collection 17 = 9+2+6 (level 2) ...
49.6 Mbytes of cons cells used (52%)
12.5 Mbytes of vectors used (16%)
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 927153 49.6 1792975 95.8 1387691 74.2
Vcells 1627339 12.5 10146329 77.5 8388601 64.0
>
> # Memory after gc(verbose = TRUE)
> memory.size(max=F)
[1] 75.8
>
> # bytes_allocated after gc(verbose = TRUE)
> default_memory_pool()$bytes_allocated
[1] 0
>
> # max_memory after gc(verbose = TRUE)
> default_memory_pool()$max_memory
[1] 83176320
>
> gc(verbose = TRUE)
Garbage collection 18 = 9+2+7 (level 2) ...
49.6 Mbytes of cons cells used (52%)
12.5 Mbytes of vectors used (16%)
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 927081 49.6 1792975 95.8 1387691 74.2
Vcells 1627219 12.5 10146329 77.5 8388601 64.0
>
> # bytes_allocated after gc(verbose = TRUE)
> default_memory_pool()$bytes_allocated
[1] 0
>
> # max_memory after gc(verbose = TRUE)
> default_memory_pool()$max_memory
[1] 83176320
>
> # Memory after gc(verbose = TRUE)
> memory.size(max=F)
[1] 75.8
>
> rm(ds)
>
> # Memory after deletion df
> memory.size(max=F)
[1] 75.8
>
> # bytes_allocated after deletion df
> default_memory_pool()$bytes_allocated
[1] 0
>
> # max_memory after deletion df
> default_memory_pool()$max_memory
[1] 83176320
>
> gc(verbose = TRUE)
Garbage collection 19 = 9+2+8 (level 2) ...
49.6 Mbytes of cons cells used (52%)
12.5 Mbytes of vectors used (16%)
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 926997 49.6 1792975 95.8 1387691 74.2
Vcells 1627193 12.5 10146329 77.5 8388601 64.0
>
> # Memory after gc(verbose = TRUE)
> memory.size(max=F)
[1] 75.8
>
> # bytes_allocated after gc(verbose = TRUE)
> default_memory_pool()$bytes_allocated
[1] 0
>
> # max_memory after gc(verbose = TRUE)
> default_memory_pool()$max_memory
[1] 83176320
>
> gc(verbose = TRUE)
Garbage collection 20 = 9+2+9 (level 2) ...
49.6 Mbytes of cons cells used (52%)
12.5 Mbytes of vectors used (16%)
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 926988 49.6 1792975 95.8 1387691 74.2
Vcells 1627178 12.5 10146329 77.5 8388601 64.0
>
> # Memory after gc(verbose = TRUE)
> memory.size(max=F)
[1] 75.8
>
> # bytes_allocated after gc(verbose = TRUE)
> default_memory_pool()$bytes_allocated
[1] 0
>
> # max_memory after gc(verbose = TRUE)
> default_memory_pool()$max_memory
[1] 83176320
1 - After loading all the necessary libraries:
- memory.size() = 90.74
- default_memory_pool()$bytes_allocated = 0
- default_memory_pool()$max_memory = 0
2 - After using data.frame to create the object df :
- memory.size() = 133.23
- default_memory_pool()$bytes_allocated = 0
- default_memory_pool()$max_memory = 0
There is no use of the arrow function, I think I understand that this is why the values of $bytes_allocated and $max_memory are not impacted?
3 - After using arrow::write_dataset :
- memory.size() = 120.07
- default_memory_pool()$bytes_allocated = 19000128
- default_memory_pool()$max_memory = 27126592
Using the arrow function affects the values of $bytes_allocated and $max_memory
4 - After deleting the df object and gc() :
- memory.size() = 74.35
- default_memory_pool()$bytes_allocated = 0
- default_memory_pool()$max_memory = 27126592
I don't understand why default_memory_pool()$bytes_allocated = 0 after deleting df, when it was 0 when creating df and 19000128 after arrow::write_dataset. Shouldn't it be 19000128?
5 - After using arrow::open_dataset when creating the ds object :
- memory.size() = 79.01
- default_memory_pool()$bytes_allocated = 0
- default_memory_pool()$max_memory = 27126592
Using the arrow function when creating ds does not, this time, affect the values of $bytes_allocated and $max_memory. Why not?
6 - After passing the contents of ds and using collect() to create the req object :
- memory.size() = 84.46
- default_memory_pool()$bytes_allocated = 47504192
- default_memory_pool()$max_memory = 83176320
Using the arrow function again impacts the values of $bytes_allocated and $max_memory. Why not?
7 - After deleting the req object and gc() :
- memory.size() = 75.8
- default_memory_pool()$bytes_allocated = 0
- default_memory_pool()$max_memory = 83176320
Deleting the req object affects the value of $bytes_allocated
8 - After deleting the ds object and gc() :
- memory.size() = 75.8
- default_memory_pool()$bytes_allocated = 0
- default_memory_pool()$max_memory = 83176320
I don't quite understand how $bytes_allocated and $max_memory work. Could you please explain?
Thanks for running these! bytes_allocated is the current number of bytes of Arrow memory that have been allocated; max_memory is the maximum number of bytes that were allocated at any given time.
The output you've pasted makes sense to me:
- Writing a dataset will convert to a
Tablefirst, so may require some allocating from Arrow's side. After the dataset is written, the Table is deleted and the original data frame remains (but no Arrow-allocated memory exists). open_dataset()does not do any allocating on Arrow's side, it just specifies a dataset that could be scanned without scanning it- Collecting a dataset into a data.frame does not by itself remove Arrow references: many of the vectors that make up the resulting data.frame are shells around Arrow memory. It is not until you remove the data.frame and garbage collect that the memory is freed.
Thank you for this information and your help.
The problem I have is that I still can't understand this difference in memory used between windows task manager and memory.size() or default_memory_pool()$bytes_allocated...
Hello,
After trying on linux (debian), I also get memory release issues after using gc() on functions using the arrow library, looking at the rsession element of the top command.
Below are the different captures:
- The script used with output :
> gc(verbose = TRUE)
Garbage collection 5 = 3+0+2 (level 2) ...
26.3 Mbytes of cons cells used (46%)
6.1 Mbytes of vectors used (9%)
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 490588 26.3 1068414 57.1 666531 35.6
Vcells 792200 6.1 8388608 64.0 1820111 13.9
>
> # basic memory
> # memory.size(max=F)
> library(pryr)
> mem_used()
45 MB
>
> library(arrow, warn.conflicts = FALSE)
Some features are not enabled in this build of Arrow. Run `arrow_info()` for more information.
>
> # Memory after loading the arrow library with memory.size
> # memory.size(max=F)
> mem_used()
57.7 MB
>
> # bytes_allocated after loading the arrow library
> # default_memory_pool()$bytes_allocated
>
> # max_memory after loading the arrow library
> # default_memory_pool()$max_memory
>
> library(dplyr)
Attachement du package : ‘dplyr’
L'objet suivant est masqué depuis ‘package:pryr’:
where
Les objets suivants sont masqués depuis ‘package:stats’:
filter, lag
Les objets suivants sont masqués depuis ‘package:base’:
intersect, setdiff, setequal, union
>
> # Memory after loading the dplyr library with memory.size
> # memory.size(max=F)
> mem_used()
62.8 MB
>
> # bytes_allocated after loading the dplyr library
> # default_memory_pool()$bytes_allocated
>
> # max_memory after loading the dplyr library
> # default_memory_pool()$max_memory
>
> df <- data.frame(
+ col1 = rnorm(1000000),
+ col2 = rnorm(1000000),
+ col3 = runif(1000000),
+ col4 = sample(1:999, size = 1000000, replace = T),
+ col5 = sample(c("GroupA", "GroupB"), size = 1000000, replace = T),
+ col6 = sample(c("TypeA", "TypeB"), size = 1000000, replace = T)
+ )
>
> # Memory after df object creation
> # memory.size(max=F)
> mem_used()
107 MB
>
> # bytes_allocated after df object creation
> # default_memory_pool()$bytes_allocated
>
> # max_memory after df object creation
> # default_memory_pool()$max_memory
>
> arrow::write_dataset(
+ df,
+ "~/test",
+ format = "parquet"
+ )
>
> # Memory after writing to disk
> # memory.size(max=F)
> mem_used()
119 MB
>
> # bytes_allocated after writing to disk
> # default_memory_pool()$bytes_allocated
>
> # max_memory after writing to disk
> # default_memory_pool()$max_memory
>
> rm(df)
>
> # Memory after deletion df
> # memory.size(max=F)
> mem_used()
74.6 MB
>
> # bytes_allocated after deletion df
> # default_memory_pool()$bytes_allocated
>
> # max_memory after deletion df
> # default_memory_pool()$max_memory
>
> gc(verbose = TRUE)
Garbage collection 19 = 10+0+9 (level 2) ...
57.9 Mbytes of cons cells used (63%)
13.3 Mbytes of vectors used (17%)
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 1083548 57.9 1707185 91.2 1707185 91.2
Vcells 1739307 13.3 10076106 76.9 8500943 64.9
>
> # Memory after gc(verbose = TRUE)
> # memory.size(max=F)
> mem_used()
74.6 MB
>
> # bytes_allocated after gc(verbose = TRUE)
> # default_memory_pool()$bytes_allocated
>
> # max_memory after gc(verbose = TRUE)
> # default_memory_pool()$max_memory
>
> gc(verbose = TRUE)
Garbage collection 21 = 10+0+11 (level 2) ...
57.9 Mbytes of cons cells used (63%)
13.3 Mbytes of vectors used (17%)
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 1083548 57.9 1707185 91.2 1707185 91.2
Vcells 1739307 13.3 10076106 76.9 8500943 64.9
>
> # Memory after gc(verbose = TRUE)
> # memory.size(max=F)
> mem_used()
74.6 MB
>
> # bytes_allocated after gc(verbose = TRUE)
> # default_memory_pool()$bytes_allocated
>
> # max_memory after gc(verbose = TRUE)
> # default_memory_pool()$max_memory
>
> ds <- arrow::open_dataset("~/test")
>
> # Memory after ds creation
> # memory.size(max=F)
> mem_used()
79.2 MB
>
> # bytes_allocated after ds creation
> # default_memory_pool()$bytes_allocated
>
> # max_memory after ds creation
> # default_memory_pool()$max_memory
>
> req <-
+ ds %>%
+ collect()
>
> # Memory after req creation
> # memory.size(max=F)
> mem_used()
80.6 MB
>
> # bytes_allocated after req creation
> # default_memory_pool()$bytes_allocated
>
> # max_memory after req creation
> # default_memory_pool()$max_memory
>
> rm(req)
>
> # Memory after deletion req
> # memory.size(max=F)
> mem_used()
80.6 MB
>
> # bytes_allocated after deletion req
> # default_memory_pool()$bytes_allocated
>
> # max_memory after deletion req
> # default_memory_pool()$max_memory
>
> gc(verbose = TRUE)
Garbage collection 26 = 10+0+16 (level 2) ...
62.5 Mbytes of cons cells used (56%)
14.5 Mbytes of vectors used (19%)
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 1168741 62.5 2088622 111.6 1707185 91.2
Vcells 1891012 14.5 10076106 76.9 8500943 64.9
>
> # Memory after gc(verbose = TRUE)
> # memory.size(max=F)
> mem_used()
80.6 MB
>
> # bytes_allocated after gc(verbose = TRUE)
> # default_memory_pool()$bytes_allocated
>
> # max_memory after gc(verbose = TRUE)
> # default_memory_pool()$max_memory
>
> gc(verbose = TRUE)
Garbage collection 28 = 10+0+18 (level 2) ...
62.5 Mbytes of cons cells used (56%)
14.5 Mbytes of vectors used (19%)
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 1168741 62.5 2088622 111.6 1707185 91.2
Vcells 1891012 14.5 10076106 76.9 8500943 64.9
>
> # bytes_allocated after gc(verbose = TRUE)
> # default_memory_pool()$bytes_allocated
>
> # max_memory after gc(verbose = TRUE)
> # default_memory_pool()$max_memory
>
> # Memory after gc(verbose = TRUE)
> # memory.size(max=F)
> mem_used()
80.6 MB
>
> rm(ds)
>
> # Memory after deletion df
> # memory.size(max=F)
> mem_used()
80.6 MB
>
> # bytes_allocated after deletion df
> # default_memory_pool()$bytes_allocated
>
> # max_memory after deletion df
> # default_memory_pool()$max_memory
>
> gc(verbose = TRUE)
Garbage collection 31 = 10+0+21 (level 2) ...
62.5 Mbytes of cons cells used (56%)
14.5 Mbytes of vectors used (19%)
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 1168648 62.5 2088622 111.6 1707185 91.2
Vcells 1890971 14.5 10076106 76.9 8500943 64.9
>
> # Memory after gc(verbose = TRUE)
> # memory.size(max=F)
> mem_used()
80.6 MB
>
> # bytes_allocated after gc(verbose = TRUE)
> # default_memory_pool()$bytes_allocated
>
> # max_memory after gc(verbose = TRUE)
> # default_memory_pool()$max_memory
>
> gc(verbose = TRUE)
Garbage collection 33 = 10+0+23 (level 2) ...
62.5 Mbytes of cons cells used (56%)
14.5 Mbytes of vectors used (19%)
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 1168648 62.5 2088622 111.6 1707185 91.2
Vcells 1890971 14.5 10076106 76.9 8500943 64.9
>
> # Memory after gc(verbose = TRUE)
> # memory.size(max=F)
> mem_used()
80.6 MB
>
> # bytes_allocated after gc(verbose = TRUE)
> # default_memory_pool()$bytes_allocated
>
> # max_memory after gc(verbose = TRUE)
> # default_memory_pool()$max_memory
-
top (before using the script) => %MEM of rsession =1.8
-
top (after using script with rm() and gc()) => %MEM of rsession =5.2
-
free -h (before using script) => mem used : 2.2Gi
-
free -h (after using script with rm() and gc()) => mem used : 2.4Gi
Do you think this problem could be linked to the arrow library or the R language?
Best regards,
Things I would think to investigate:
- It's possible that bytes_allocated is 0 and arrow has "freed" the memory which gives it back to the allocator (jemalloc, malloc). These allocators do not necessarily give the memory back to the operating system (either due to fragmentation or because it is saving the memory to use again later). Do you know if you are using jemalloc or the system allocator?
- It's possible that more memory is being used because of the
library(dplyr),library(arrow),library(pryr)commands.
Hello westonpace,
Thank you for your reply and the information you've given me.
How can I find out which allocator I'm using?
Do you know if there is a way to free the memory that has been consumed to declare the different libraries with library()?
Best regards,
Kevin
How can I find out which allocator I'm using?
arrow::arrow_info()
...
Memory:
Allocator system
Current 0 bytes
Max 0 bytes
Do you know if there is a way to free the memory that has been consumed to declare the different libraries with library()?
I do not, unfortunately.
Do you know if there is a way to free the memory that has been consumed to declare the different libraries with library()?
I am pretty sure that this isn't available...there are ways to track R allocations in certain sections of code using the profmem package but it doesn't extend to C or C++ alllocations that happen outside of R vectors.
This reads like an XY problem. To avoid miscommunication: @kevinpemonon what is your specific problem? Are you running an arrow script on a machine and running out of memory or something? If you suspect or have observed something like a memory leak a reprex would be great (e.g. a script that can get R oom killed where it would usually not be expected).
Memory management, as well explained by Weston and Dewey is quite complex and often not as straight forward as one would think e.g. memory is kept allocated to a program like R by the os even if it is not needed at the moment to avoid costly operations (shuffling things around in ram to defrag) though this memory will be freed once it is requested by another program (but not if there are x GB free that can be used). And the environment the code is run on also plays a roll.
All of this makes very general exploratory questions hard to answer.