R的保留内存是分配数组大小的两倍 [英] Reserved memory of R is twice the size of an allocated array

查看:104
本文介绍了R的保留内存是分配数组大小的两倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了以下行为.假设我创建了以下多维数组:

I spotted the following behavior. Say I create the following multi-dimensional array:

spam = array(runif(96*48*60*360), dim = c(96,48,60,360))

这完全可以预见,R应该为此使用多少内存,即(96 * 48 * 60 * 360)* 4字节= 759.4 MB.使用lsos函数可以很好地确认这一点(请参见这篇文章):

It is quite predictable how much memory R should use for this, namely (96*48*60*360) * 4 bytes = 759.4 Mbyte. This is nicely confirmed using the lsos function (see this post):

> lsos()
         Type      Size PrettySize Rows Columns
spam    array 796262520   759.4 Mb   96      48
lsos function       776  776 bytes   NA      NA

R作为一个进程使用更多的内存,大约是它的两倍:

R as a process however uses much more memory, roughly twice the size:

$ top | grep rsession
82:17628 hiemstra  20   0 1614m **1.5g** 8996 S  0.3 40.4   0:04.85 rsession  

R为什么要这样做?我假设分配了额外的保留内存,以使R可以更快地访问它?有什么想法吗?

Why does R do this? I assume the extra reserved memory is allocated to make it more quickly accessible to R? Any thought's?

推荐答案

因为垃圾收集器尚未运行.
因此,有很多垃圾,很可能是在创建大数组的过程中产生的,必须清除.

Because the garbage collector has not run yet.
So there's a lot of garbage, probably generated during the creation of the big array, that has to be cleared.

如果通过调用gc()函数强制进行垃圾回收,则会看到已使用的内存将非常接近数组的大小:

If you force a garbage collection by calling gc() function, you will see that the used memory will be pretty near to the size of your array:

> memory.size()
[1] 775.96

这篇关于R的保留内存是分配数组大小的两倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆