protect(): protection stack overflow

Sep 30, 2014 at 4:04 PM
I got the following error:
RDotNet.EvaluationException: Error: protect(): protection stack overflow
at RDotNet.REngine.Parse(String statement, StringBuilder incompleteStatement)
at RDotNet.REngine.<Defer>d__0.MoveNext()
at System.Linq.Enumerable.LastOrDefault[TSource](IEnumerable`1 source)
at RDotNet.REngine.Evaluate(String statement)
This happened while running something like this:
_rEngine.Evaluate("newcol <- c(...values...)").AsNumeric();
I.e. creating a new column with some values, which is then used to create a DataFrame. I did not change the code for some time and it always worked correctly. It only threw after few days of running. That's why (and also because of the err. message) it looks like there was some memory limit reached.

I create a new linear regression model and data frames every time the application accesses R - could this be the issue? How do I clean the memory once done with the model/data-frame?

Oct 1, 2014 at 1:31 PM
General R troubleshooting:
What does executing Cstack_info() after you get the exception tell you?
Are you using 32 bit R and how big are your datasets?
If so, have you tried 64 bit R?

R.Net troubleshooting:
Are you disposing of the datatypes that are returned when you are done with them? (Not the rengine?)
Is there a way to map less types to C# types and do more inside of evaluate?
Similarly, could you just read the R console to get the values you want (as strings, instead of disposable C#/R types)?
Oct 7, 2014 at 12:16 PM
Hi there, thanks for advices!

1.) How can I get the output of Cstack_info() call - from C#? I.e (related to your last advice) how do I read the console of R from C#?

2.) I am using 64 bit version of R, the datasets are not very big - up to 3000 rows (and about 10 columns)

3.) I am not disposing manually of anything - but neither I hold the data so they should be garbage collected, right (the DataFrames are not disposable)?
Oct 8, 2014 at 12:03 AM
1) Wrap it in a print() call, like this:
class Program
    static void Main( string[] args )
        using (var engine = REngine.GetInstance())
            engine.Evaluate("newcol <- c(1,2,3,4,5); print(newcol); print(Cstack_info())");
R.Net installs a console device that writes to Console.Out. Unfortunately, R does not autoprint when embedded, so you have to wrap things in a print call. I've submitted a PR for autoprint support in R.Net.

2) It could be how the data.frame is established. The protection stack size is currently set to 50000, which is close to the 30k items in your dataset. Again, its hard to tell without more code, even if that code just uses random data.

3) As long as you have C# objects that represent R variables, you are probably have items in the protection stack. Again, this is a very general statement and without specific code, its hard to tell.
Oct 10, 2014 at 12:46 PM
Thanks skyguy94,

Actually, the datasets are 3k items, but I run this in other configurations where the datasets are 10 times bigger, and the exception did not occur.

I added the Cstack_info() calls and will have more info if this happens again (did not since then).