What happens to memory allocated by C functions in

2020-07-27 12:11发布

问题:

In my R package , the C code which implements the function allocates some memory for usage. What happens to this memory ? Will it be under R's GC's radar or would it be a memory leak ? If its under R's garbage collection , will this memory be reclaimed back ?

I have a huge 5 GB of data to be allocated in R's server process which is running as a server. Here it would be a good idea if i can allocate this memory out of GC's radar like we have outside heap memory allocation in Java. So basically , can i allocate a huge amount of memory in my C code without R not disturbing that memory ?

I am planning to use malloc or calloc to allocate memory.

回答1:

This is described fairly clearly in "Writing R Extensions" -- one uses R variants of calloc() / malloc() that access the same pool of memory. That way R can gc() these things, and why you need PROTECT() annd UNPROTECT().

6.1.2 User-controlled memory

The other form of memory allocation is an interface to malloc, the interface providing R error handling. This memory lasts until freed by the user and is additional to the memory allocated for the R workspace.

The interface functions are

 TYPE* Calloc(size_t N, TYPE)
 TYPE* Realloc(ANY *P, size_t N, TYPE)
 void Free(ANY *P)

providing analogues of calloc, realloc and free. If there is an error during allocation it is handled by R, so if these routines return the memory has been successfully allocated or freed. Free will set the pointer P to NULL. (Some but not all versions of S do so.)

Users should arrange to Free this memory when no longer needed, including on error or user interrupt. This can often be done most conveniently from an on.exit action in the calling R function - see pwilcox for an example.

Do not assume that memory allocated by Calloc/Realloc comes from the same pool as used by malloc: in particular do not use free or strdup with it.

These entry points need to be prefixed by R_ if STRICT_R_HEADERS has been defined.



回答2:

From Chambers' Software for Data Analysis:

Whenever any computation could possibly allocate R storage dynamically and the result is not assigned at the R level, the use of the corresponding C reference must be protected by the PROTECT and UNPROTECT macros. The reason is that the storage would otherwise be released in R happened to do a garbage collection of dynamic storage during the computation.