From lgautier at gmail.com  Sat Feb 23 15:07:37 2008
From: lgautier at gmail.com (Laurent Gautier)
Date: Sat, 23 Feb 2008 22:07:37 +0800
Subject: [Rd] Calling R_PreserveObject from embedded R
Message-ID: <27d1e6020802230607t72e317abv3218f4530185c3c8@mail.gmail.com>

>  Hello. This is my first post to the list, so first I'd like to thank
>  everybody for making and mantaining such a great product as R.
>  I'm writting a native binding to R from Dolphin Smalltalk. I've followed up
>  the examples of the documentation showing how to run R embedded, and I got
>  it partially working. However, I have a problem with the reference handling
>  of the R objects.
>  I've followed this strategy: every time I call a function in R and it
>  answers me a SEXP, I called R_PreserveObject(sexp), and wrap it with a
>  Smalltalk object. Whenever the Smalltalk object dies, I release the R object
>  by calling R_ReleaseObject(sexp).
>  This seems to handle well the life cycle, but makes the running process to
>  use a growing and a never ending amount of memory. Actually, after
>  experimenting a while, I isolated the problem to iterate over a loop which
>  all it does is create an expresion for a number, call PreserveObject and
>  call ReleaseObject, and that alone makes the memory to grow indefinitely.
>  I couldn't find any comment about this behaviour. Is there something I'm
>  missing ?

Martin,

I am working with code taking a similar approach (from Python), and
I do not seem to observe what you are experiencing.

When I initialize the embeded R with the "--verbose" flag, and initialize
disposable objects from a loop in Python, R outputs lines such as the
ones below:
11.8 Mbytes of vectors used (40%)
Garbage collection 536 = 307+159+70 (level 0) ...
7.1 Mbytes of cons cells used (38%)
19.4 Mbytes of vectors used (65%)
Garbage collection 537 = 307+160+70 (level 1) ...
7.1 Mbytes of cons cells used (38%)
11.8 Mbytes of vectors used (40%)

>  I'm running the embedded R-2.6.2 binaries for Windows in a WindowsXP sp2
>  environment.

I am currently working on Linux.

>  I've also implemented another strategy, which keeps a global list in R, and
>  instead of calling R_PreserveObject, it inserts the SEXP in the list. This
>  made the memory usage problem to go away, but the performance is noticeably
>  worst compared with the other strategy, and is not as elegant as the first
>  one neither, so I was hoping to be able to use the first strategy.

The first strategy appears is something similar, if I understood it right
(there is a global SEXP object called R_PreciousList in memory.c).

>  I'll appreciate any comment about what might be going on.

May be try  the "--verbose" flag ?

>  Thanks in advance.
>
>  Best regards.
>  martin
>



[Rd] Calling R_PreserveObject from embedded R
Laurent Gautier