I am using xlsx package to create workbook, worksheet, write data to the sheet and then save the workbook. However, when I repeat this set of actions for several times, I start to the error message:
>Error in .jcheck(silent = FALSE) :
java.lang.OutOfMemoryError: GC overhead limit exceeded.jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, .jcast(if (inherits(o, "jobjRef") || inherits(o, "jarrayRef")) o else cl, "java/lang/Object"), .jnew("java/lang/String", method), j_p, j_pc, use.true.class = TRUE, evalString = simplify, evalArray = FALSE)<S4 object of class "jobjRef"
>Error in .jnew("java/io/FileOutputStream", file) :
Java Exception <no description because toString() failed>.jnew("java/io/FileOutputStream", file)<S4 object of class "jobjRef">
>Error in .jnew("org/apache/poi/xssf/usermodel/XSSFWorkbook") :
Java Exception <no description because toString() failed>.jnew("org/apache/poi/xssf/usermodel/XSSFWorkbook")<S4 object of class "jobjRef">
Error in .jcheck(silent = FALSE) :
Java Exception <no description because toString() failed>.jcall(wb, "Lorg/apache/poi/ss/usermodel/Sheet;", "createSheet", sheetName)<S4 object of class "jobjRef"
I guess this is because repetition of the actions consume the Java memory.
My question is, how can I clear/reinitialize the memory so that I can repeat these actions again without having the out of memory errors?
Is there any simple code in r can achieve this?
Thanks a lot for your advice!
Piggy-backing the above answer to note that you don't need XLConnect
to accomplish the effects of xlcFreeMemory
... just note that what xlcFreeMemory
actually does is to run the following:
gc()
J("java.lang.Runtime")$getRuntime()$gc()
That is, it runs gc()
first in your R environment and then in the existing Java instance's environment.
I do not know how to do it on xlsx, in fact I no longer use it because of those issues
I now use XLConnect, and it has a command
xlcFreeMemory()
to do just that
Please note that for best results you need to increase Java memory before youload it as follows
options( java.parameters = "-Xmx4g" )
library(XLConnect)
Good luck
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With