Function appears to crash and take 4GB of RAM

setClassUnion("missing_or_numeric", c("numeric", "missing"))
setGeneric("my_function",
           function(t, e, N, mu, alpha, beta, p) {
             standardGeneric("my_function")
           })

setMethod("my_function",
          signature(t = "vector", e = "vector",
            beta  = "vector", p = "missing_or_numeric"),
          function(t, e, N, mu, alpha, beta, 
                   p) {
            print("function entered")
            if (missing(p)) p <- t # p was not missing when run

            A = matrix(0, nrow=N, ncol= length(t)) # N was 3 when run.
            lam_p <- rep(0,length(p)) # p was of length 100 when run.
            
            print("Prelim done")
            for (j in 2:length(t)) {
              # A Loop, with moderately heavy calculations ...

I've been having problems with the above chunk of code. This is nested inside another function (but only called once). Upon it being called, RStudio uses about 4GB of RAM and the function does not finish even after half an hour. I have to terminate R each time I call it. None of the print statements are printed, despite the simplicity of the opening lines of code in the function- so that suggests none of the lines are being executed at all. The profiler is unable to break down time/memory usage by line.
The RAM usage is independent of the lengths of vectors t and e, which have ranged from about 300 to 30,000. The other inputs are very small memory-wise.
I've had no previous issues with RStudio, and I've been using another very similiar function successfully on a regular basis. It seems to me that something is stopping this function from executing, and that simultaneously a lot of RAM is being used. Any ideas on what this could be?
Thanks in advance,
Alec

Update: After removing the signature, it works, even though there's nothing apparently wrong with the signature.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.