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!

r
Edit
Report