减少 Java 运行时的最大堆栈大小
我正在尝试编写一个junit测试来防止一段代码陷入无休止的迭代中,这最终会导致StackOverflow。
所以我正在寻找一种方法来减少运行时的堆栈大小,以便 Junittest 更快地失败。
将最大堆栈设置为 jvm 参数是不可能的,因为该测试是更大的测试套件的一部分。
I'm attempting to write a junit test to guard against a piece of code getting stuck in a endless iteration which will eventually cause a StackOverflow.
so i'm looking for a way to decrease the stack size at runtime so the Junittest will fail faster.
setting the max stack as a jvm argument is not possible because the test is part of a much larger test suite.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以运行一个递归方法,该方法将运行给定的次数,然后执行给定的操作。 不过听起来很不稳定:(
类似这样的东西:
编辑:智能 JVM 可能会在这里优化尾部调用,所以我们可能需要在递归调用之后做“一些事情”来阻止这种情况的发生...
Ick 'n东西:(
You could run a recursive method which will run itself a given number of times and then execute a given action. Sounds pretty flaky though :(
Something like:
EDIT: It's possible that smart JVMs will optimise the tail call here, so it may be that we'd need to do "something" after the recursive call to stop that from happening...
Ick 'n stuff :(
不可能在运行时设置堆栈大小,但也许您可以:
thread.getStackTrace()
,如果其大小大于x,则失败;未编译的概念验证代码(未正确检查所有边缘条件):
It's not possible to set the stack size at runtime, but perhaps you can:
thread.getStackTrace()
and fail if its size is larger than x;not-compiled proof of concept code ( does not properly check all edge conditions ):
您只能在启动时设置这样的参数,但是您可以从 Java 启动另一个进程。 因此,您可以让单元测试启动具有较小堆栈大小的第二个进程来执行测试。
You can only set parameters like this at startup, however you can start another process from Java. So you could make your unit tests start a second process with a smaller stack size to perform your test.
这有点……好吧,有趣……但这可能是值得的。
This gets a bit... well, interesting... but it might be worth it.