[R-sig-Geo] memory leaking of some rgeos functions?

Roger Bivand Roger.Bivand at nhh.no
Sat Oct 29 15:07:54 CEST 2011


On Sat, 29 Oct 2011, Roman Luštrik wrote:

> Dear list,
>
> I've been looking into this and it would seem that some function in rgeos,
> namely gBuffer and gDifference, are leaking memory. After the functions
> finish, they don't release RAM. I've described this behavior in this
> post<http://stackoverflow.com/questions/7927977/r-memory-leaking-using-rgeosgdifference-gbuffer>and
> would appreciate if someone (Roger, Colin?) could take a look.

Roman,

Please understand that if you choose to post on stackoverflow, you fork 
the information flow very seriously. Forcing us to go there to see what is 
happening is not OK for me. Usually, blogs and stackoverflow are 
overflowing with non-authoritative advice (which authors have not 
investigated adequately before publishing), which is often simply wrong.

Forking open source communities is a very serious choice, please don't. If 
you need to document a query, put it somewhere where follow-up comes to 
the list posting thread in which you link to the external document.

You omitted to report your GEOS version. I am running GEOS runtime 
version: 3.3.1-CAPI-1.7.1 on RHEL5 x64, and don't see abnormal behaviour, 
but also see that memory allocated in your example is freed only when R 
exits.

The R memory manager behaves differently between platforms, and is much 
less forgiving on Windows, because the underlying technology differs from 
OSX and Linux/Unix. I guess Windows users are running the CRAN rgeos/GEOS 
binary, but I am not sure what its version is - GEOS memory leaks get 
addressed in new releases, and are present, hard to fix, and generally not 
prioritised by the GEOS team - there are more pressing problems.

In reply to one SO comment, yes, we have run valgrind, but are not in a 
position to resolve issues that may be in GEOS. Where we have been able to 
see that the issue is in rgeos, it has been dealt with, but it should be 
understood that GEOS is not simple, is C++, and often does things which 
seem odd when using the recommended C API.

In general, any memory allocated in rgeos uses the R memory allocation 
system, so is potentially seen by the garbage collector, as this shows:

$ grep alloc src/*.[ch]

It may be that the objects are seen as still in use, or that we have not 
been sufficiently dilligent in destroying C++ objects in GEOS. I would 
also be hesitant to say that only gBuffer() and gDifference() are 
implicated here, because the underlying mechanisms are similar for most 
functions - as you would see if you had looked at the C source code.

So rather than expressing surprise, please do read and audit the source 
carefully, and once you have definitely found the cause, please offer a 
patch. Examples just at the R level really don't show us anything, I'm 
afraid. Perhaps look at the use of the GEOS C API in PostGIS for hints.

Hope this clarifies,

Roger

>
> Cheers,
> Roman
>
>
>
>

-- 
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no


More information about the R-sig-Geo mailing list