以自动方式查找泄漏内存的JUnit测试 [英] Automated way to find JUnit tests that leak memory

查看:173
本文介绍了以自动方式查找泄漏内存的JUnit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们问题的根源是单身人士。但单身人士很难打破,与此同时我们有很多单元测试使用单身人士而不用小心在tearDown()方法中完全清除它们。我认为检测这些测试的好方法是寻找内存泄漏。如果在tearDown()和System.gc()之后使用的内存大于测试开始时使用的内存,则测试泄漏或类加载器加载更多类。有没有办法自动检测这类问题?

The root of our problem is Singletons. But Singletons are hard to break and in the meantime we have a lot of unit tests that use Singletons without being careful to completely clear them in the tearDown() method. I figure that a good way to detect tests like these is to look for memory leaks. If the memory used after tearDown() and System.gc() is more than when the test started, either the test leaked or more classes were loaded by the classloader. Is there any way to automatically detect this sort of problem?

推荐答案

你能介绍一下TestCase和各个测试类之间的子类吗?那做了清理工作?然后子类只负责调用super.teardown() - 并且只负责调用自己的拆卸()。

Could you introduce a subclass, between TestCase and your individual test classes, that did the cleanup? Then subclasses would only be responsible for calling super.teardown() - and only those that had a teardown() of their own.

这篇关于以自动方式查找泄漏内存的JUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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