RStudio中报告的矩阵对象的内存使用情况
假设我有一个大型矩阵RStudio中报告的矩阵对象的内存使用情况,r,memory,rstudio,profiling,R,Memory,Rstudio,Profiling,假设我有一个大型矩阵x,类型为numeric,包含1e4*1e4个元素 x应该需要1e8*8/1e6=800MB内存(加上一些头)。这由object.size()和pryr::object\u size()确认: >x对象大小(x) 80000216字节 >pryr::对象大小(x) 800 MB 但是,“环境”选项卡中报告的大小为762.9MB RStudio如何计算RStudio的“环境”选项卡中报告的对象的内存使用量?差异来自何处?RStudio显示的内存大小与object.size相同
x
,类型为numeric
,包含1e4*1e4个元素
x
应该需要1e8*8/1e6=800MB内存(加上一些头)。这由object.size()
和pryr::object\u size()
确认:
>x对象大小(x)
80000216字节
>pryr::对象大小(x)
800 MB
但是,“环境”选项卡中报告的大小为762.9MB
RStudio如何计算RStudio的“环境”选项卡中报告的对象的内存使用量?差异来自何处?RStudio显示的内存大小与
object.size
相同。MB包含1024KB,KB包含1024B:
object.size(x)
# 800000216 bytes
object.size(x) / 1024 / 1024
# 762.9 bytes
我试着查找pryr::object\u size
代码,但它是用C实现的。我查看了,字节的计算结果如下:
double bytes = 0;
// Big vectors always allocated in 8 byte chunks
if (n_bytes > 16) bytes = n_bytes * 8;
// For small vectors, round to sizes allocated in small vector pool
else if (n_bytes > 8) bytes = 128;
else if (n_bytes > 6) bytes = 64;
else if (n_bytes > 4) bytes = 48;
else if (n_bytes > 2) bytes = 32;
else if (n_bytes > 1) bytes = 16;
else if (n_bytes > 0) bytes = 8;
return bytes;
}
所以这可能就是为什么你的计算是800MBs,与pryr匹配
double bytes = 0;
// Big vectors always allocated in 8 byte chunks
if (n_bytes > 16) bytes = n_bytes * 8;
// For small vectors, round to sizes allocated in small vector pool
else if (n_bytes > 8) bytes = 128;
else if (n_bytes > 6) bytes = 64;
else if (n_bytes > 4) bytes = 48;
else if (n_bytes > 2) bytes = 32;
else if (n_bytes > 1) bytes = 16;
else if (n_bytes > 0) bytes = 8;
return bytes;
}