0

Imagine a scenario where there is a function that takes a very large matrix M as one of its inputs. The function performs some action on M obtaining another matrix which is then used for a subsequent step programmed in the function. If M is only used once in the function, is it possible to execute an instruction from within the function that completely deallocates M from memory?

I know that doing something like M <- NULL inside the function will not deallocate M in the global environment of R, in fact I can't think of any way that comes remotely close to achieving what I'm describing here. It would be weird as the user would find his inputs gone once the function finishes executing, but this would be beneficial for processes that handle large data structures.

MikeKatz45
  • 545
  • 5
  • 16
  • 2
    `M <<- NULL`? Sounds like this is possibly premature optimization. – John Coleman Mar 03 '21 at 00:41
  • is this <<- operator sort of like a way to modify the object by reference? – MikeKatz45 Mar 03 '21 at 00:42
  • See [How do you use "<<'" in R?](https://stackoverflow.com/q/2628621/4996248). Personally, I haven't found a good use-case for it, but if you are pushing against memory limits, perhaps it will help. The crucial question is when garbage collection kicks in after that. I don't know enough about R internals to answer. – John Coleman Mar 03 '21 at 00:46

1 Answers1

2

There might be different ways to do this. One way that comes to mind would be to store the object in an environment, so it can be removed from the environment inside the function:

uses.the.object <- function(e) {
  m <- e$m
  
  # Do something with it
  
  # Delete it
  m <- NULL
  e$m <- NULL
}

e <- new.env()
e$m <- numeric(1000*1000)
uses.the.object(e)
Owen
  • 38,836
  • 14
  • 95
  • 125
  • interesting, didnt know about environments. This could be a good option, only downside I see is that someone using the function would have to create the environment and submit that as input instead of just inputing the arguments directly as usual – MikeKatz45 Mar 03 '21 at 00:53
  • @MikeKatz45 That's true. If you don't want to create a separate environment, values *created* in function calls live only in the functions local environment and not in the caller's environment (e.g., `uses.the.object(numeric(1000))`). However, that would not work if the object were already created before the function call. – Owen Mar 03 '21 at 00:58