“Out of Memory Error (Java)” when using R and XLCo

2020-01-24 07:24发布

I tried to load a ~30MB excel spreadsheet into R using the XLConnect package.

This is what I wrote:

wb <- loadWorkbook("largespreadsheet.xlsx")

And after about 15 seconds, I got the following error:

Error: OutOfMemoryError (Java): GC overhead limit exceeded.

Is this a limitation of the XLConnect package or is there a way to tweak my memory settings to allow for larger files?

I appreciate any solutions/tips/advice.

标签: excel r memory
7条回答
Melony?
2楼-- · 2020-01-24 07:29

In case someone encounters this error when reading not one huge but many files, I managed to solve this error by freeing Java Virtual Machine memory with xlcFreeMemory(), thus:

files <- list.files(path, pattern = "*.xlsx")
for (i in seq_along(files)) {
    wb <- loadWorkbook(...)
    ...
    rm(wb)
    xlcFreeMemory()  # <= free Java Virtual Machine memory !
}
查看更多
老娘就宠你
3楼-- · 2020-01-24 07:29

As suggested in this here, make sure to run the option function in the first line in your code. In my case, it worked only when I restarted the R session and run it in the first line.

options(java.parameters = "-Xmx4g" )
library(XLConnect)
查看更多
不美不萌又怎样
4楼-- · 2020-01-24 07:30

Whenever you are using a library that relies on rJava (such as RWeka in my case), you are bound to hit the default heap space (512 MB) some day. Now, when you are using Java, we all know the JVM argument to use (-Xmx2048m if you want 2 gigabytes of RAM). Here it's just a matter of how to specify it in the R environnement.

   options(java.parameters = "-Xmx2048m")
   library(rJava)
查看更多
别忘想泡老子
5楼-- · 2020-01-24 07:33

If you still have problems with importing XLSX files you can use this opiton. Anwser with "Xmx1024m" didn't work and i changed to "-Xmx4g".

options(java.parameters = "-Xmx4g" )
library(XLConnect)

This link was useful.

查看更多
Emotional °昔
6楼-- · 2020-01-24 07:35

Use read.xlsx() in the openxlsx package. It has no dependency on rJava thus only has the memory limitations of R itself. I have not explored in much depth for writing and formatting XLSX but it has some promising looking vignettes. For reading large spreadsheets, it works well.

Hat tip to @Brad-Horn. I've just turned his comment as an answer because I also found this to be the best solution!

查看更多
别忘想泡老子
7楼-- · 2020-01-24 07:51

This appears to be the case, when u keep using the same R-session over and over again without restarting R-Studio. Restarting R-Studio can help to allocate a fresh memory-heap to the program. It worked for me right away.

查看更多
登录 后发表回答