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.
This is described fairly clearly in "Writing R Extensions" -- one uses R variants of
malloc() that access the same pool of memory. That way R can
gc() these things, and why you need
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
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.
Freewill set the pointer P to
NULL. (Some but not all versions of S do so.)
Users should arrange to
Freethis memory when no longer needed, including on error or user interrupt. This can often be done most conveniently from an
on.exitaction in the calling R function - see
pwilcoxfor an example.
Do not assume that memory allocated by
Realloccomes from the same pool as used by
malloc: in particular do not use
These entry points need to be prefixed by
STRICT_R_HEADERShas been defined.