0

I am currently attempting to use R to read a (large, 8.3 MB) .xlsx file into a matrix. I am attempting to do so with the read.xlsx file in the xlsx package. https://cran.r-project.org/web/packages/xlsx/index.html

I am now trying to read the contents of one of the sheets in the file with the following command:

sheetname<-read.xlsx("/Users/jinkinsonsmith/Downloads/Re _Introduction/filename.xlsx",sheetName='sheetname')

It looks like this command should work in terms of reading the contents of sheet "sheetname" in xlsx file "filename" into the vector "sheetname". However, instead, I am getting this error message:

Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, : java.lang.OutOfMemoryError: Java heap space

It seems like I'm not the first person to get this error message (example: How to deal with "java.lang.OutOfMemoryError: Java heap space" error?), but even after reading the other post I just linked it is still not clear to me what I should do to fix this error. My MacBook Pro has long had issues with running out of disk space and requiring me to delete a bunch of files, so that could be the culprit, but it is also apparently possible that I have too many stored references to objects in R that I no longer use and that are taking up too much space. In the latter case I don't know how I would remove any unneeded references.

NCC1701
  • 139
  • 11
Jinkinson
  • 13
  • 5
  • You can change to `R` in `64bits` mode. – Duck Aug 03 '20 at 17:33
  • Have you tried any of the other packages for reading XL files? excel.link, readexcel, readxlsb, XLCONNECT perhaps. Javaheapspace is RAM, not disk. Consider also reading a specified number of rows and repeating the call for the next set of rows, etc. – Carl Witthoft Aug 03 '20 at 18:25
  • Try the `read_xlsx` from the `openxlsx` package. It does not rely on java as package `xlsx` does. – Paul van Oppen Aug 03 '20 at 21:15
  • @PaulvanOppen I assume you mean "read.xlsx", rather than "read_xlsx", which appears to be part of the "readxl" package not the "openxlsx" package. Good news: read.xlsx worked! – Jinkinson Aug 04 '20 at 01:51
  • Yes, you are correct: `read.xlsx` – Paul van Oppen Aug 04 '20 at 05:17

1 Answers1

0

By using the following line of code before you load any other package, I could solve similar problems like this. I already described it here.

options(java.parameters = c("-XX:+UseConcMarkSweepGC", "-Xmx8192m"))
library(xlsx)

Please add this line and restart, since other packages can load some java things by themselves and the options have to be set before any Java is loaded.

In general, these option change the type of garbage collection which sometimes makes problems in the default settings and also increases the memory to 8GB.

drmariod
  • 11,106
  • 16
  • 64
  • 110