當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R object.size 報告為對象分配的空間

R語言 object.size 位於 utils 包(package)。

說明

提供用於存儲的內存的估計值R對象。

用法

object.size(x)

## S3 method for class 'object_size'
format(x, units = "b", standard = "auto", digits = 1L, ...)
## S3 method for class 'object_size'
print(x, quote = FALSE, units = "b", standard = "auto",
      digits = 1L, ...)

參數

x

一個R對象。

quote

邏輯,指示是否應使用引號打印結果。

units

用於格式化和打印尺寸的單位。不同 standard 的允許值為

standard = "legacy"

"b" , "Kb" , "Mb" , "Gb" , "Tb" , "Pb" , "B" , "KB" , "MB" , "GB" , "TB""PB"

standard = "IEC"

"B" , "KiB" , "MiB" , "GiB" , "TiB" , "PiB" , "EiB" , "ZiB""YiB"

standard = "SI"

"B" , "kB" , "MB" , "GB" , "TB" , "PB" , "EB" , "ZB" , "YB" , "RB""QB"

對於所有標準,units = "auto"也是允許的。如果standard = "auto"、任何 "legacy" 和IEC單位是允許的。有關詳細信息,請參閱“格式化和打印對象尺寸”。

standard

要使用的byte-size 單位標準。字符串,可能縮寫為 "legacy""IEC""SI""auto" 。有關詳細信息,請參閱“格式化和打印對象尺寸”。

digits

小數點後的位數,傳遞給 round

...

要傳遞給其他方法或從其他方法傳遞的參數。

細節

確切地說內存分配的哪些部分應該歸屬於哪個對象不是clear-cut。此函數僅提供粗略指示:它對於原子向量應該相當準確,但不會檢測例如列表的元素是否共享。 (考慮字符向量元素之間的共享,但不考慮單個對象中字符向量之間的共享。)

計算的是對象的大小,不包括在符號表中存儲其名稱所需的空間。

相關空間(例如,函數的環境以及 EXTPTRSXP 中的指針所指向的內容)不包括在計算中。

64 位構建上的對象大小比 32 位構建上的大,但在具有相同字長和指針大小的不同平台上很可能是相同的。

使用緊湊內部表示的對象的大小可以是over-estimated。

"object_size" 的對象,具有長度為 1 的雙精度值,可歸因於該對象的內存分配的估計值(以字節為單位)。

格式化和打印對象尺寸

對象大小可以使用 byte-size 單位進行格式化R的遺留標準,IEC標準,或SI標準。如下表所示,傳統和IEC標準使用二進製單位(1024 的倍數),而 SI 標準使用十進製單位(1000 的倍數)。

對於方法 formatprint ,參數 standard 指定要使用的標準,參數 units 指定要使用的 byte-size 單元。 units = "auto" 選擇結果為 1 或更多的最大單位(四舍五入之前)。字節大小四舍五入到 digits 小數位。如果可能,standard = "auto" 根據 units 選擇標準,否則使用舊標準。

的總結R的遺產和IEC單位:

物體大小 legacy IEC
1 1字節1乙
1024 1 KB1 KiB
1024^21兆字節1 MiB
1024^31GB1 GiB
1024^41 TB1 TiB
1024^51 鉛1 PiB
1024^6 1 EiB
1024^7 1 ZiB
1024^8 1 YiB

的總結SI單位:

物體大小 SI
1 1乙
1000 1 KB
1000^21MB
1000^31GB
1000^41TB
1000^51PB
1000^61 EB
1000^71ZB
1000^81 元
1000^91 RB
1000^101 個四分衛

例子

object.size(letters)
object.size(ls)
format(object.size(library), units = "auto")

sl <- object.size(rep(letters, 1000))

print(sl)                                    ## 209288 bytes
print(sl, units = "auto")                    ## 204.4 Kb
print(sl, units = "auto", standard = "IEC")  ## 204.4 KiB
print(sl, units = "auto", standard = "SI")   ## 209.3 kB

(fsl <- sapply(c("Kb", "KB", "KiB"),
               function(u) format(sl, units = u)))
stopifnot(identical( ## assert that all three are the same :
             unique(substr(as.vector(fsl), 1,5)),
             format(round(as.vector(sl)/1024, 1))))

## find the 10 largest objects in the base package
z <- sapply(ls("package:base"), function(x)
            object.size(get(x, envir = baseenv())))
if(interactive()) {
as.matrix(rev(sort(z))[1:10])
} else # (more constant over time):
    names(rev(sort(z))[1:10])

作者

R Core; Henrik Bengtsson for the non-legacy standards.

參考

The wikipedia page, https://en.wikipedia.org/wiki/Binary_prefix, is extensive on the different standards, usages and their history.

也可以看看

Memory-limits 用於對象大小的設計限製。

相關用法


注:本文由純淨天空篩選整理自R-devel大神的英文原創作品 Report the Space Allocated for an Object。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。