[R] About R memory management?

Peng Yu pengyu.ut at gmail.com
Fri Dec 11 03:11:44 CET 2009


Thatis was not my original question. My original questions was how
memory is managed/allocated in R?

On Thu, Dec 10, 2009 at 6:08 PM, jim holtman <jholtman at gmail.com> wrote:
> If you really want to code like a C++ coder in R, then create your own
> object and extend it when necessary:
>
> # take a variation of this; preallocate and then extend when you read a
> limit
> x <- numeric(2)
> for (i in 1:100){
>     if (i > length(x)){
>         # double the length (or whatever you want)
>         length(x) <- length(x) * 2
>     }
>     x[i] <- i
> }
>
> On Thu, Dec 10, 2009 at 11:30 AM, Peng Yu <pengyu.ut at gmail.com> wrote:
>>
>> I have a situation that I can not predict the final result's dimension.
>>
>> In C++, I believe that the class valarray could preallocate some
>> memory than it is actually needed (maybe 2 times more). The runtime
>> for a C++ equivalent (using append) to the R code would still be C*n,
>> where C is a constant and n is the length of the vector. However, if
>> it just allocate enough memory, the run time will be C*n^2.
>>
>> Based on your reply, I suspect that R doesn't allocate some memory
>> than it is currently needed, right?
>>
>> On Fri, Dec 11, 2009 at 11:22 AM, Henrik Bengtsson <hb at stat.berkeley.edu>
>> wrote:
>> > Related...
>> >
>> > Rule of thumb:
>> > Pre-allocate your object of the *correct* data type, if you know the
>> > final dimensions.
>> >
>> > /Henrik
>> >
>> > On Thu, Dec 10, 2009 at 8:26 AM, Peng Yu <pengyu.ut at gmail.com> wrote:
>> >> I'm wondering where I can find the detailed descriptions on R memory
>> >> management. Understanding this could help me understand the runtime of
>> >> R program. For example, depending on how memory is allocated (either
>> >> allocate a chuck of memory that is more than necessary for the current
>> >> use, or allocate the memory that is just enough for the current use),
>> >> the performance of the following program could be very different.
>> >> Could somebody let me know some good references?
>> >>
>> >> unsorted_index=NULL
>> >> for(i in 1:100) {
>> >>  unsorted_index=c(unsorted_index, i)
>> >> }
>> >> unsorted_index
>> >>
>> >> ______________________________________________
>> >> R-help at r-project.org mailing list
>> >> https://stat.ethz.ch/mailman/listinfo/r-help
>> >> PLEASE do read the posting guide
>> >> http://www.R-project.org/posting-guide.html
>> >> and provide commented, minimal, self-contained, reproducible code.
>> >>
>> >
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem that you are trying to solve?
>




More information about the R-help mailing list