Restart engine

Aug 19, 2013 at 6:46 AM
engine works well and during the process I create a huge number of variables. Now I want to run another independent program and I need to clear all the variables I create before. So I want to restart my engine. The restart also useful when an error occured and the best choice is to restart the engine.

I use the following to realize it but failed:

engine.close()
engine.Initialize();

However, it reported that "Subscripts out of bounds"

Do you have any other ideas to restart the engine? or I will restart my EXE to solve the problem, it's really clumsy.
Aug 19, 2013 at 11:02 AM
I also facing with this problem. ı need to solve this problem too
Sep 10, 2013 at 1:28 AM
I also have this problem, I even try ---
                            rEngine.Close();
                            rEngine.Dispose();
                            rEngine = null;

                            rEngine = REngine.CreateInstance("RDotNet");
                            rEngine.Initialize();
But it just hangs (without error) on .Initalize()

Experts please help!
Sep 10, 2013 at 5:27 AM
Edited Sep 10, 2013 at 5:28 AM
I have also tried to clear the memory for the rEngine without restarting (though restarting does seem like a surefire solution).

When I try and clear my memory I use -
                            rEngine.Evaluate("rm(price_dataframe)");
                            rEngine.Evaluate("rm(trans_price_dataframe)");
                            rEngine.Evaluate("rm(tsdist)");
                            rEngine.Evaluate("rm(hc)");
                            rEngine.Evaluate("rm(cl)");
                            rEngine.Evaluate("gc()");
( those are all the objects I am using in R )

then,
                    GC.Collect();
                    rEngine.ForceGarbageCollection();
I get to about the 200th iteration of my routine ( using R to cluster a series of changing vectors ), before "Error: memory exhausted (limit reached?)" using the above.

Either if I could clear and restart rEngine ( as happens when I restart my c# project ) on each 50th iteration - as the two guys above want - or otherwise I could get the memory clearing stuff to work then I would be fine.

Library is fantastic, once I get this working I will be on Cloud 9.
Sep 11, 2013 at 6:14 AM
I just tried to quit R before the rEngine.Close() command, in the hope it would help -
                            rEngine.Evaluate("q()");
But this quit out of my whole c# application!


Anyone got any ideas? I'll take workarounds + anything !!!
Developer
Sep 11, 2013 at 7:42 AM
@billybond: rm() removes references to the objects in the R world, but if some of these are still pointed to from a C# reference, it may not be possible to garbage collect them with gc(). Not sure about this one; Kosei may want to comment. Also, R does not have memory compaction and the effect of gc() on effective memory usage is not all that straightforward to intuit.
@billybond: the effect of "q()" is likely to be deep in the R engine, so not something we have control over.
Developer
Sep 11, 2013 at 7:52 AM
10d_world wrote:
engine works well and during the process I create a huge number of variables. Now I want to run another independent program[...].
If it is an independent program, which should mean different process, this is a different R session. You probably mean something else here.

On the topic of restarting R with a clear session, there's probably some good know how in RStudio to learn from (https://github.com/rstudio/rstudio/tree/master). Feature request taken in consideration; question is whether it can be resourced over other tasks.
Coordinator
Sep 12, 2013 at 7:06 AM
R.NET <= 1.5.2 had a bug of memory leaks: R.NET does not release native handles so that GCs cannot collect them. If you use R.NET >= 1.5.3, I have no idea without a reproducible code.
Sep 13, 2013 at 2:03 AM
kos-
You were right! Upgraded and no longer have memory issue. Sorry to trouble you!

jperraud-
The ability to restart with a"clear" session would be simply magnificent. It seems strange that the only way to do this is to close and restart the .NET compiled EXE file.

both-
Fantastic library guys - the best way I have found to access R from .NET by far: truly a great contribution to the community!

Donald
Sep 13, 2013 at 4:48 AM
REngine would be perfect if it can be interrupted and restarted like RStudio and R-GUI.
Nov 13, 2014 at 9:53 AM
So actually it is still IMPOSSIBLE for REngine to restart without restarting the whole c# project?? I could not find anywhere a positive answer.

I am hosting a website with IIS using .NET. My users expect to access the data from backend and use R to process them. Without the restarting of R Engine means the R session is never over until IIS is restarted. There must be a better way!
Nov 13, 2014 at 11:11 AM
No, it is not impossible.

But I don't understand your problem. If every user wants to use R, then why not simply use the same R session to handle all their requests? If your code base in R need a new session for each request, that suggests a more immediate problem.

Hacky work arounds are still possible. A basic challenge is that due to how R is setup, bad code in it can "take down" an entire process (i.e. your C# application). The most robust work around is to start R in a separate process, and run your code through an interface layer that does interprocess communication. This way if R dies, you can catch it on the C# side, and just kick off another process, R won't be able to disrupt your C# code. Of course the new version might die to, so you might repeatedly be hacking around a problem in R.

This is how the F# R type provider does things though. It creates a new process it communicates with to process R requests, and it is reasonably robust.

So two questions: 1 - are you loading the R runtime in the same process and 2 - Why do all R calls require independent processes?
Nov 13, 2014 at 3:57 PM
So actually it is still IMPOSSIBLE for REngine to restart without restarting the whole c# project?
Yes, R.Net simply wraps the unmanaged R dlls so that you can call them from a .Net process. There are ways to push the R.Net wrapper and R out of the IIS process using WCF, .Net remoting, or even DCOM, but these techniques are not a part of R.Net.
Nov 14, 2014 at 7:29 AM
Edited Nov 14, 2014 at 7:30 AM
evolvedmicrobe wrote:
So two questions: 1 - are you loading the R runtime in the same process and 2 - Why do all R calls require independent processes?
Actually my application does not require independent R sessions, you are totally right. I can use one R session for my whole c# application, just to make sure to clear out all the objects to return the memory after the calculation process. So far I am loading R in the same process. I disabled the interactive R scripting from user input in case of bad coding. If I could start R in a separate process, then everything should work as a charm. Thanks a lot for the great tips!