在包中运行所有测试时,JUnit java.lang.OutOfMemoryError [英] JUnit java.lang.OutOfMemoryError when running all tests in a package

查看:209
本文介绍了在包中运行所有测试时,JUnit java.lang.OutOfMemoryError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将所有单元测试加载到包中时,make任务将引发java.lang.OutOfMemoryError:Java堆空间错误.

When loading all unit tests in a package, the make task throws a java.lang.OutOfMemoryError: Java heap space error.

但是,如果我在每个子包中运行所有测试,则所有测试都可以加载并完成.仅当我尝试在父程序包中运行所有测试时,才会发生OOM错误.

If I run all the tests in each subpackage, though, all tests load and complete just fine. It is only when I try to run all tests in the parent package that the OOM error occurs.

我认为不应该通过调整VM参数来解决此问题.我尝试增加最大堆和烫发大小,但并没有解决问题.

I don't think this problem should be solved by tweaking VM parameters. I tried increasing the maximum heap and perm size, and it didn't solve the problem.

这使我相信在不同程序包的加载测试之间存在一些垃圾收集问题,或者正在进行一些过于急切的类加载.

This leads me to believe there is some problem garbage collecting between loading tests in different packages, or that there is some too-eager class loading going on.

是否存在可以解决这些问题的JUnit设置,还是必须通过在测试用例中更改或添加代码来解决问题?

Is there a JUnit setting that could take care of these issues, or is the problem going to have to be solved by changing or adding code in the test cases?

推荐答案

您必须在tearDown()中将测试类的所有字段都设置为null.

You must set all fields of the test classes to null in tearDown().

原因是JUnit实例化每个测试 的测试类的一个实例.它会一直保留该实例,以保存测试结果(成功,失败,堆栈跟踪).因此,如果您使用字段,它们将保留并且将耗尽内存.

The reason is that JUnit instantiates one instance of the test class per test. It keeps that instance around for the whole time to save the results of the test (success, failure, stack trace). So if you use fields, they will stay and you'll run out of memory.

这篇关于在包中运行所有测试时,JUnit java.lang.OutOfMemoryError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆