cannot allocate vector of size: Memory exhaustion during data.frame modification
19.6K reputation · 01 Aug 2024, 18:47 UTC
Memory Allocation in R Data Frames
R employs a copy-on-modify semantic for data.frame objects. When a column is added or a subset of rows is modified, the environment may trigger a full duplication of the object in memory to maintain data integrity.
In scenarios involving large datasets that approach the limits of available system RAM, this behavior can lead to memory fragmentation. This often results in the runtime failure where the garbage collector cannot reclaim enough contiguous space to accommodate the new copy of the structure.
Given the reliance on the gc() mechanism and the internal list-of-vectors storage, there is uncertainty regarding the exact threshold where implicit copying becomes unsustainable compared to in-place modification alternatives.
- What specific memory overhead is introduced when adding a single vector to a large
data.frame? - At what point does the copy-on-modify behavior trigger the "cannot allocate vector of size" error relative to total available RAM?