There is a lot to get one's head around, and I will not bet I got the sole issue, but the following lines do not look right.
Second and importantly, calling SetHandleAsInvalid, presumably under the assumption it will help release memory, likely has the exact opposite effect:. See SafeHandle.SetHandleAsInvalid Method for the behavior.
What I think happens is that because you call
Hope this helps to alleviate the issue.
I'll see whether this is feasible and desirable to hide these SafeHandle methods from users.
prdPxVector.SetHandleAsInvalid();
prdPxVector.Dispose();
First, though not all that important, prdPxVector is created in the "using" statement, so no need to call Dispose: it is called anyway.Second and importantly, calling SetHandleAsInvalid, presumably under the assumption it will help release memory, likely has the exact opposite effect:. See SafeHandle.SetHandleAsInvalid Method for the behavior.
What I think happens is that because you call
SetHandleAsInvalid
, when disposing of prdPxVector
in C# it does not trigger the decrement of the reference count in the R world. So, when you call e.g. "rm(SCORE_FIELD)"
, the R garbage collector still sees at least one reference to SCORE_FIELD. And all the previously created SCORE_FIELD over the loop and probably other variables still cannot be garbage collected, hence the memory blow-out.Hope this helps to alleviate the issue.
I'll see whether this is feasible and desirable to hide these SafeHandle methods from users.