[Rd] optim/vmmin and R_alloc

Göran Broström gb at tal.stat.umu.se
Thu Dec 30 16:55:13 CET 2004


I am calling 'vmmin' several times from a C function (which is called via
.C). It works very well, except for memory consumption. The cause is that
vmmin allocates memory via R_alloc, and this memory is not freed as vmmin
exits. Instead all the allocated memory is freed on return of the .C
call. 

In one application, I have 2000 functions of 500 variables each to
minimize. In each call to vmmin, about 1MB memory is allocated,
which sums up to 2GB in total allocation. I have 2.4GB memory in my office
computer, and it is all used up. It seems as if garbage collection saves
the situation at the end, but the slow down is noticeable. On my home
computer with 512MB memory, this problem cannot be run.

However, and that is my question/suggestion: Why not use Calloc+Free
instead of R_alloc in vmmin (and maybe in other places)? I made the
changes in 'optim.c' in the functions 'vmmin' and 'Lmatrix'. Then I rebuilt
and reinstalled R-2.0.1. The result was great! My function ran very fast and
without any memory problems.

So why not? Or will this change create other problems?
 
-- 
 Göran Broström                    tel: +46 90 786 5223
 Department of Statistics          fax: +46 90 786 6614
 Umeå University                   http://www.stat.umu.se/egna/gb/
 SE-90187 Umeå, Sweden             e-mail: gb at stat.umu.se



More information about the R-devel mailing list