R 메모리 관리 / 크기 n Mb의 벡터를 할당 할 수 없음
R에서 큰 객체를 사용하는 데 문제가 있습니다. 예를 들면 다음과 같습니다.
> memory.limit(4000)
> a = matrix(NA, 1500000, 60)
> a = matrix(NA, 2500000, 60)
> a = matrix(NA, 3500000, 60)
Error: cannot allocate vector of size 801.1 Mb
> a = matrix(NA, 2500000, 60)
Error: cannot allocate vector of size 572.2 Mb # Can't go smaller anymore
> rm(list=ls(all=TRUE))
> a = matrix(NA, 3500000, 60) # Now it works
> b = matrix(NA, 3500000, 60)
Error: cannot allocate vector of size 801.1 Mb # But that is all there is room for
나는 이것이 인접한 메모리 블록을 얻는 것이 어렵다는 것을 이해합니다 ( here ).
크기의 벡터를 할당 할 수없는 오류 메시지는 크기가 프로세스의 주소 공간 제한을 초과했거나 시스템이 메모리를 제공 할 수 없었기 때문에 메모리를 확보하지 못했음을 나타냅니다. 32 비트 빌드에는 사용 가능한 충분한 여유 메모리가있을 수 있지만이를 맵핑 할 수있는 충분히 인접한 주소 공간 블록은 없습니다.
이 문제를 어떻게 해결할 수 있습니까? 내 주요 어려움은 스크립트의 특정 지점에 도달하고 R이 객체에 200-300 Mb를 할당 할 수 없다는 것입니다. 다른 처리를 위해 메모리가 필요하기 때문에 실제로 블록을 미리 할당 할 수 없습니다. 불필요한 객체를 제거 할 때에도 마찬가지입니다.
편집 : 예, 죄송합니다 : Windows XP SP3, 4Gb RAM, R 2.12.0 :
> sessionInfo()
R version 2.12.0 (2010-10-15)
Platform: i386-pc-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_Caribbean.1252 LC_CTYPE=English_Caribbean.1252
[3] LC_MONETARY=English_Caribbean.1252 LC_NUMERIC=C
[5] LC_TIME=English_Caribbean.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
이 데이터를 모두 명시 적으로 필요로하는지 또는 행렬이 희소 할 수 있는지 고려하십시오. Matrix
희소 행렬에 대해 R에서 좋은 지원이 있습니다 ( 예 : 패키지 참조 ).
이 크기의 객체를 만들어야하는 경우 R의 다른 모든 프로세스와 객체를 최소로 유지하십시오. 사용 gc()
하지 않는 메모리를 지우거나 한 세션에서 필요한 객체 만 만드는 것이 좋습니다 .
위의 방법으로 문제가 해결되지 않으면 RAM 용량이 충분한 64 비트 시스템을 구입하여 64 비트 R을 설치하십시오.
그렇게 할 수 없다면 원격 컴퓨팅을위한 많은 온라인 서비스가 있습니다.
그렇게 할 수 없다면 package ff
(또는 bigmemory
Sascha가 언급 한) 와 같은 메모리 매핑 도구 가 새로운 솔루션을 구축하는 데 도움이됩니다. 제한된 경험으로 ff
는 고급 패키지이지만 High Performance Computing
CRAN 작업보기에 대한 주제를 읽어야합니다 .
Windows 사용자의 경우 다음은 일부 메모리 제한 사항을 이해하는 데 도움이되었습니다.
- R을 열기 전에 Windows 자원 모니터를여십시오 (Ctrl-Alt-Delete / 작업 관리자 시작 / 성능 탭 / 하단의 '자원 모니터'/ 메모리 탭 클릭).
- R을 열기 전에 이미 사용한 RAM 메모리 양과 응용 프로그램을 확인할 수 있습니다. 필자의 경우 총 4GB 중 1.6GB가 사용됩니다. 따라서 R의 경우 2.4GB 만 얻을 수 있지만 이제는 더 나빠집니다 ...
- R을 열고 1.5GB의 데이터 세트를 만든 다음 크기를 0.5GB로 줄이면 리소스 모니터에 RAM이 거의 95 % 사용 된 것으로 표시됩니다.
gc()
가비지 수집을 수행 하는 데 사용 => 작동, 메모리 사용이 2GB로 감소 함을 볼 수 있습니다.
내 컴퓨터에서 작동하는 추가 조언 :
- prepare the features, save as an RData file, close R, re-open R, and load the train features. The Resource Manager typically shows a lower Memory usage, which means that even gc() does not recover all possible memory and closing/re-opening R works the best to start with maximum memory available.
- the other trick is to only load train set for training (do not load the test set, which can typically be half the size of train set). The training phase can use memory to the maximum (100%), so anything available is useful. All this is to take with a grain of salt as I am experimenting with R memory limits.
Here is a presentation on this topic that you might find interesting:
http://www.bytemining.com/2010/08/taking-r-to-the-limit-part-ii-large-datasets-in-r/
I haven't tried the discussed things myself, but the bigmemory
package seems very useful
The simplest way to sidestep this limitation is to switch to 64 bit R.
I encountered a similar problem, and I used 2 flash drives as 'ReadyBoost'. The two drives gave additional 8GB boost of memory (for cache) and it solved the problem and also increased the speed of the system as a whole. To use Readyboost, right click on the drive, go to properties and select 'ReadyBoost' and select 'use this device' radio button and click apply or ok to configure.
If you are running your script at linux environment you can use this command:
bsub -q server_name -R "rusage[mem=requested_memory]" "Rscript script_name.R"
and the server will allocate the requested memory for you (according to the server limits, but with good server - hugefiles can be used)
나는 memor.limit의 도움말 페이지를 따랐고 내 컴퓨터 R에서 기본적으로 최대 1.5GB의 RAM을 사용할 수 있으며 사용자 가이 제한을 늘릴 수 있음을 알았습니다. 다음 코드를 사용하여
>memory.limit()
[1] 1535.875
> memory.limit(size=1800)
내 문제를 해결하는 데 도움이되었습니다.
위에서 언급 한 저장 /로드 방법이 저에게 효과적입니다. gc()
메모리 조각 모음이 어떻게 / 확실하지는 않지만 잘 작동하는 것 같습니다.
# defrag memory
save.image(file="temp.RData")
rm(list=ls())
load(file="temp.RData")
참고 URL : https://stackoverflow.com/questions/5171593/r-memory-management-cannot-allocate-vector-of-size-n-mb
'programing tip' 카테고리의 다른 글
JAX-WS, Axis2 및 CXF의 차이점 (0) | 2020.07.05 |
---|---|
cscope 또는 ctags 왜 다른 것을 선택합니까? (0) | 2020.07.05 |
jQuery를 사용하여 event.preventDefault ()를 호출하는 리스너를 바인딩 해제하는 방법은 무엇입니까? (0) | 2020.07.05 |
CMAKE가 오류를 발생시키는 원인 (0) | 2020.07.05 |
C ++ 자동 키워드. (0) | 2020.07.05 |