[R] Memory Problems

Harry Mangalam mangalam at home.com
Mon Nov 22 15:46:22 CET 1999


As a biologist who joined the dark side and started programming, I agree
with the previous assessments - you've got a memory leak.  So now what? 
I've found that one of the best ways of finding it and fixing it have been
with the dmalloc library by Gray Watson.  It's reasonably simple to install
and use, requires minimal changes in your code (have to add one #include
and link to the dmalloc lib) and is actaully makes memory debugging
relatively easy.  You can get the lib and docs from:

http://www.dmalloc.com/

despite the name, it's free (small 'f') software.  You can also try
Electric Fence which is distributed as part of a number of linux
distributions, but I found that to be not as powerful nor as easy to use.

You may have to write a C wrapper for your C routine (but then you probably
already have one, for your own debugging, no?).

This only makes sense, of course, if the R alloc routines behave in an
identical way as the C equivs (or you can use the C equivs in the included
code - I'm sorry - I've never done this in R).  Corrections PLEASE.

Cheers
harry


Joanna Mills wrote:
> 
> Hi,
> I am sorry to bother you both again but still seem to have the same
> problem and am in dire need of a solution. As you can imagine, taking
> 2+G of our system doesn't make me very popular :-)
> 
> To give you a few more details, my program only passes from R to C once.
> All of the computations are basically done in C, this is where the memory
> seems to be being 'eaten' up. At present I have calloc and cfree
> statements, I tried replacing with R_alloc, no change.
> When I replaced with C_alloc and C_free I got a message saying
> 'all C pointers in use (sorry)'. I tried Init_C_alloc and Reset_C_alloc
> but with no success.
> 
> Do either of you have any other suggestions?
> 
> Thank you both very much for your time,
> Joanna
> 
> Joanna Elizabeth MILLS, M.Sc.               Ph:  (902) 494-3747
> Dalhousie University                        Fax: (902) 494-5130
> Department of Mathematics and Statistics    Res: (902) 443-0230
> Halifax, N.S. Canada B3H 3J5                millsje at mscs.dal.ca
> 
> On Fri, 19 Nov 1999, Martyn Plummer wrote:
> 
> > On 19-Nov-99 Peter Dalgaard BSA wrote:
> > > Joanna Mills <millsje at mscs.dal.ca> writes:
> > >
> > >> I have an R program which basically reads in data. It then
> > >> calls a  C program which performs numerical integration until converging
> > >>  to a solution.  The result is then passed back from C to R.
> > >> Unfortunately, when running this program, the virtual
> > >> memory associated with it explodes -> 2GIG!. Does anyone know what might
> > >> be causing this or can suggest possiblie ways to debug?
> > >
> > > Sounds as if the C routine is allocating memory and not freeing it?
> > >
> > > You may want to check up on R_alloc (now where did we document that? I
> > > can find various hints in help(Memory), help(dyn.load), and help(.C),
> > > and in the R-external that will be in the upcoming release, but
> > > nothing really coherent.)
> >
> > There is some documentation of R_alloc in the source file
> > src/main/memory.c which was enough to get me started.
> >
> > The nice thing about R_alloc is that memory is freed automatically by the
> > .C interface if you forget to do it. Even better, you can use the error()
> > function to exit gracefully from your C code when there is a problem,
> > and the R_alloc'ed memory will also be freed. I don't think this is documented.
> >
> > Martyn
> >
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-- 
Cheers,
Harry

Harry J Mangalam -- (949) 856 2847 -- mangalam at home.com
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list