Java 为什么只有在运行 JUnit 测试时堆空间才会耗尽?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/681133/
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 17:50:22  来源:igfitidea点击:

Why does heap space run out only when running JUnit tests?

javaeclipsejunitout-of-memory

提问by smauel

When running JUnit tests, I always seem to run into this error:

在运行 JUnit 测试时,我似乎总是遇到这个错误:

eclipse outOfMemoryError: heap space

eclipse outOfMemoryError:堆空间

I have monitored Eclipse with JConsole and heap memory peaks at about 150MB. I have set heap memory to 1GB.

我使用 JConsole 监控 Eclipse,堆内存峰值约为 150MB。我已将堆内存设置为 1GB。

I am using the following arguments when starting Eclipse:

我在启动 Eclipse 时使用以下参数:

-vm "C:\Program Files\Java\jre1.5.0_08\bin\javaw.exe" -vmargs -Xmx1024M
-XX:MaxPermSize=128M -Dcom.sun.management.jmxremote.port=8999
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false

Does anyone know what may be causing this issue? It happens only when running JUnit tests.

有谁知道可能导致此问题的原因?它仅在运行 JUnit 测试时发生。

采纳答案by Thijs Wouters

Junit tests are run in a different vm as the Eclipse IDE. So it is that vm that is out of memory and not the Eclipse one.
You can change the settings of the test vm in the run configurations of the test.
You go to the run configurations and then under arguments, you can set the vm arguments.

Junit 测试在与 Eclipse IDE 不同的虚拟机中运行。所以是那个 vm 内存不足,而不是 Eclipse 那个。
您可以在测试的运行配置中更改测试虚拟机的设置。
您转到运行配置,然后在参数下,您可以设置 vm 参数。

回答by Michael Borgwardt

You probably have a memory leak in your JUnit tests. A common gotcha is this: Junit will create a new instance of a TestCase class for every test method in itAnd all instance variables will be kept around until JUnit terminates. That means: if you have a TestCase class with 50 test methods and an instance variable that is initialized with a 1MB object graph in your setUp() method, then that TestCase class will require 50MB heap space.

您的 JUnit 测试中可能存在内存泄漏。一个常见的问题是:Junit 将为其中的每个测试方法创建一个 TestCase 类的新实例,并且所有实例变量都将保留,直到 JUnit 终止。这意味着:如果您有一个包含 50 个测试方法的 TestCase 类和一个在 setUp() 方法中使用 1MB 对象图初始化的实例变量,那么该 TestCase 类将需要 50MB 堆空间。

Edit:the problem described above only exists in older versions of JUnit, I think it was fixed in JUnit 4.

编辑:上述问题仅存在于旧版本的 JUnit 中,我认为它已在 JUnit 4 中修复。

回答by Joey Gibson

I've just released a plugin for Eclipse that will automatically set the heap size on JUnit launchers for you. You can get it from http://code.google.com/p/junitlaunchfixer/It works with Eclipse Europa, Ganymede and Galileo.

我刚刚发布了一个 Eclipse 插件,它会自动为您设置 JUnit 启动器上的堆大小。您可以从http://code.google.com/p/junitlaunchfixer/获得它。它适用于 Eclipse Europa、Ganymede 和 Galileo。

回答by Ana

I found the solution to my problem - it may help others ;) When I was increasing the heap size I was increasing the heap size of eclipse application, not of my program (which I executed through eclipse) What I had to do is modify the execution commands before running my program.

我找到了我的问题的解决方案 - 它可能会帮助其他人;) 当我增加堆大小时,我增加了 eclipse 应用程序的堆大小,而不是我的程序(我通过 eclipse 执行的)的堆大小我必须做的是修改在运行我的程序之前执行命令。

回答by Nigel_V_Thomas

Further to @Thijs Wouters response, to fix this issue in eclipse I did the following:

继@Thijs Wouters 回应后,为了在 Eclipse 中解决此问题,我执行了以下操作:

  • Added a new Run configuration under JUnit (Run>Run configuration>JUnit>New)
  • Within the arguments tab set VM arguments to "-Xms64m -Xmx256m" or higher if needs be
  • 在 JUnit 下添加了一个新的 Run 配置(Run>Run configuration>JUnit>New)
  • 在参数选项卡中,如果需要,将 VM 参数设置为“-Xms64m -Xmx256m”或更高