Wednesday, March 25, 2015

Out of Memory Exception running Java in R


I encounter this problem when I tried to run a function that execute Java to export a dataset from R to xlsx. It seems like its not a good way to export large dataset using the library(xlsx) as it may eat up the java memory. So I do I resolve this?

I have tried running the following command to expand the heap size but it does not work. options(java.parameters = "-Xmx1000m")

The way I found is to use XLConnect library.

Instead of using write.xlsx(myDataset, file = "myfile.xlsx") from the xlsx library, I used library(XLConnect).

writeWorksheetToFile(myFile, data=mydataset, sheet="mySheet")

It has a function to clear the memory, xlcFreeMemory().

If all else fails, try writing to CSV format then convert into xlsx from Excel.

No comments: