paleolimbot commented on issue #1043:
URL: 
https://github.com/apache/arrow-nanoarrow/issues/1043#issuecomment-7388775659

   I investigated this on nanoarrow 0.9.0 and current main 
(`260e1b1411ad73df422ba7f800d28a1a9b81f7dc`, 0.9.0.9000), with R 4.5.2/Linux 
and arrow 26.0.1.1. I can reproduce retention after conversion, but so far have 
not reproduced a failure to reclaim an unreachable batch.
   
   Here is a small example using Arrow's memory pool to measure live native 
allocations, rather than RSS (which need not fall when allocations are freed). 
The numeric column is explicitly int64 so its Arrow buffer is allocated by 
Arrow rather than borrowed from an R integer vector.
   
   ```r
   library(nanoarrow)
   
   pool <- arrow::default_memory_pool()
   collect <- function() for (i in 1:3) invisible(gc())
   collect()
   baseline <- pool$bytes_allocated
   live_bytes <- function() pool$bytes_allocated - baseline
   
   chunk <- as_nanoarrow_array(arrow::record_batch(
     i = arrow::Array$create(1:10000, type = arrow::int64()),
     s = rep("abc", 10000)
   ))
   collect()
   live_bytes()
   #> [1] 152072
   
   df <- as.data.frame(chunk)
   collect()
   live_bytes()
   #> [1] 152072
   nanoarrow_pointer_is_valid(chunk)
   #> [1] TRUE
   
   rm(chunk)
   collect()
   live_bytes()
   #> [1] 71384
   
   # Diagnostic use of an internal helper: materialize the lazy string column.
   nanoarrow:::nanoarrow_altrep_force_materialize(df, recursive = TRUE)
   collect()
   live_bytes()
   #> [1] 0
   
   stopifnot(
     identical(df$i, as.double(1:10000)),
     identical(df$s, rep("abc", 10000))
   )
   ```
   
   Both nanoarrow versions produced the output above; allocation sizes may 
differ across platforms. Dropping `df` instead of materializing it also returns 
allocations to baseline. In a separate 100-batch check on main, dropping both 
objects and running GC returned both Arrow's live allocations and 
`nanoarrow:::preserved_count()` to baseline on every iteration.
   
   This separates two lifetimes:
   - While `chunk` remains reachable, conversion leaves it valid and its 
buffers remain owned.
   - After dropping `chunk`, the numeric buffers can be reclaimed, but the data 
frame's ALTREP string column still needs the string buffers. Those are 
reclaimed after the column is materialized and GC runs, or after the data frame 
is dropped and GC runs.
   
   Thus a release-on-conversion option would change the ownership behavior, but 
it would not necessarily free all batch memory immediately: lazy string columns 
can still need it. The example above does not require explicitly releasing a 
pointer while another object may be borrowing from it.
   
   If the motivating DBI case retains allocations even after the batch and 
converted result are dropped and repeated GC runs, a backend-specific 
reproducer would help identify what is keeping those allocations alive.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to