R 错误:java.lang.OutOfMemoryError:Java 堆空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34624002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:51:11  来源:igfitidea点击:

R Error: java.lang.OutOfMemoryError: Java heap space

javar

提问by user3768354

I am trying to connect R to Teradata to pull data directly into R for analysis. However, I am getting the error of,

我正在尝试将 R 连接到 Teradata 以将数据直接拉入 R 进行分析。但是,我收到了以下错误,

Error in .jcall(rp, "I", "fetch", stride, block) :
  java.lang.OutOfMemoryError: Java heap space

I have tried to set up my R options to increase the max heap size of JVM by doing:

我试图通过执行以下操作来设置我的 R 选项以增加 JVM 的最大堆大小:

options(java.parameters = "-Xmx8g")

I have also tried to initialize java parameters with rJava function .jinitas: .jinit(parameters="-Xmx8g"). But still failed.

我也试图初始化rJava功能的Java参数.jinit为:.jinit(parameters="-Xmx8g")。但还是失败了。

The calculated size of the data should be approximately 3G (actually less than 3G).

计算出的数据大小应该在3G左右(实际小于3G)。

回答by tuxedopong

You need to make sure you're allocating additional memory beforeloading rJava or any other packages. Wipe the environment first (via rm(list = ls())), restart R/Rstudio if you must, and modify the options at the beginning of your script.

您需要确保加载 rJava 或任何其他包之前分配了额外的内存。首先擦除环境(通过rm(list = ls())),如果必须重新启动 R/Rstudio,并修改脚本开头的选项。

options(java.parameters = "-Xmx8000m")

options(java.parameters = "-Xmx8000m")

See for example https://support.snowflake.net/s/article/solution-using-r-the-following-error-is-returned-javalangoutofmemoryerror-gc-overhead-limit-exceeded

参见例如https://support.snowflake.net/s/article/solution-using-r-the-following-error-is-returned-javalangoutofmemoryerror-gc-overhead-limit-exceeded

回答by drmariod

I somehow had this problem in a not reproducible manner, partly solved it with -Xmx8gbut run in to problems randomly.

我不知何故以不可重现的方式遇到了这个问题,部分解决了它,-Xmx8g但随机遇到了问题。

I now found an option with a different garbage collector by using

我现在使用不同的垃圾收集器找到了一个选项

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

at the beginning of the script. So far, the problem didn't occurred again.

在脚本的开头。到此为止,问题没有再出现。

EDIT:
I figured out, we can even combine multiple java parameters. So this solution didn't failed by now.

编辑:
我发现,我们甚至可以组合多个 java 参数。所以这个解决方案现在还没有失败。

EDIT 2:So one more thing about this solution, I figured our today, it is saver to execute the optionscommand before loading any other package!!! since some of the packages load some java things by themselves and the options have to be set at the beginning of the R session!

编辑 2:关于这个解决方案的另一件事,我想我们今天,options在加载任何其他包之前执行命令是一种保护!!!因为有些包自己加载了一些 java 东西,而且这些选项必须在 R 会话开始时设置!

回答by user2961057

I added garbage collection and that solved the issue for me. I am connecting to Oracle databases using RJDBC.
simply add gc()

我添加了垃圾收集并为我解决了这个问题。我正在使用 RJDBC 连接到 Oracle 数据库。
只需添加 gc()