개체의 메모리 사용량을 확인하시겠습니까?
현재 작업 공간에서 각 개체에 사용되는 RAM 용량을 확인하고 싶습니다.이것을 하는 쉬운 방법이 있습니까?
얼마 전에 나는 여기서 이 작은 너겟을 훔쳤습니다.
sort( sapply(ls(),function(x){object.size(get(x))}))
그것은 나에게 도움이 되었습니다.
개체 크기별로
개체별로 메모리를 할당하려면 object.size()를 호출하고 관심 개체를 전달합니다.
object.size(My_Data_Frame)
전달된 인수가 변수가 아니면 따옴표로 묶이거나 getcall로 묶어야 합니다.
네임스페이스를 순환하여 다음과 같이 모든 개체의 크기를 얻을 수 있습니다.
for (itm in ls()) {
print(formatC(c(itm, object.size(get(itm))),
format="d",
big.mark=",",
width=30),
quote=F)
}
개체 유형별로
네임스페이스에 대한 메모리 사용량을 개체 유형별로 가져오려면 memory.profile을 사용하십시오.
memory.profile()
NULL symbol pairlist closure environment promise language
1 9434 183964 4125 1359 6963 49425
special builtin char logical integer double complex
173 1562 20652 7383 13212 4137 1
(memory.size()라는 다른 기능이 있지만 Windows에서만 작동하는 것 같다고 듣고 읽었습니다.MB 단위의 값만 반환하므로 세션에서 사용되는 최대 메모리를 가져오려면 memory.size(max=T)를 사용합니다.
당신은 그것을 시도할 수 있습니다.lsos()
이 질문의 기능:
R> a <- rnorm(100)
R> b <- LETTERS
R> lsos()
Type Size Rows Columns
b character 1496 26 NA
a numeric 840 100 NA
R>
이 질문은 아주 오래 전에 게시되었고 합법적인 답변을 받았습니다. 하지만 저는 gdata라는 라이브러리를 사용하여 물체의 크기를 알 수 있는 또 다른 유용한 팁을 알려드리고 싶습니다.ll()
기능.
library(gdata)
ll() # return a dataframe that consists of a variable name as rownames, and class and size (in KB) as columns
subset(ll(), KB > 1000) # list of object that have over 1000 KB
ll()[order(ll()$KB),] # sort by the size (ascending)
다른(더 예쁜) 옵션 사용dplyr
data.frame('object' = ls()) %>%
dplyr::mutate(size_unit = object %>%sapply(. %>% get() %>% object.size %>% format(., unit = 'auto')),
size = as.numeric(sapply(strsplit(size_unit, split = ' '), FUN = function(x) x[1])),
unit = factor(sapply(strsplit(size_unit, split = ' '), FUN = function(x) x[2]), levels = c('Gb', 'Mb', 'Kb', 'bytes'))) %>%
dplyr::arrange(unit, dplyr::desc(size)) %>%
dplyr::select(-size_unit)
쉽게 정렬할 수 있도록 메모리와 장치를 구분하는 data.table 함수:
ls.obj <- {as.data.table(sapply(ls(),
function(x){format(object.size(get(x)),
nsmall=3,digits=3,unit="Mb")}),keep.rownames=TRUE)[,
c("mem","unit") := tstrsplit(V2, " ", fixed=TRUE)][,
setnames(.SD,"V1","obj")][,.(obj,mem=as.numeric(mem),unit)][order(-mem)]}
ls.obj
obj mem unit
1: obj1 848.283 Mb
2: obj2 37.705 Mb
...
여기에tidyverse
사용자 환경에 있는 모든 개체의 크기를 계산하는 기반 함수:
weigh_environment <- function(env){
purrr::map_dfr(env, ~ tibble::tibble("object" = .) %>%
dplyr::mutate(size = object.size(get(.x)),
size = as.numeric(size),
megabytes = size / 1000000))
}
이 링크의 솔루션을 사용했습니다.
for (thing in ls()) { message(thing); print(object.size(get(thing)), units='auto') }
잘 작동합니다!
언급URL : https://stackoverflow.com/questions/1395270/determining-memory-usage-of-objects
'programing' 카테고리의 다른 글
오류: 다음 파일에 대한 로컬 변경 내용은 체크아웃 시 덮어씁니다. (0) | 2023.06.26 |
---|---|
Oracle 시퀀스 트랜잭션 기능 (0) | 2023.06.26 |
git 상태는 수정 사항을 표시하고 git 체크아웃 - 제거하지 않습니다. (0) | 2023.06.26 |
스프링 데이터를 통해 MongoDB에서 대규모 수집을 반복합니다. (0) | 2023.06.26 |
문제를 일으키는 db의 mongore restore (0) | 2023.06.26 |