使用Java泛型可以挂起编译器 [英] Use of Java generics could be hanging the compiler

查看:108
本文介绍了使用Java泛型可以挂起编译器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约55个Java接口和5个抽象类。每个声明使用相同的一组通用参数:它列出大约60个声明,因此每个实现知道每个其他实现的特定类型,并在方法参数中替换此类型,并适当返回。看起来这个(可能是超过)使用泛型是导致编译器挂起。编译器抛出一个OutOfMemoryError,但似乎没有终止。

I have about 55 Java interfaces and 5 abstract classes. Each declaration uses the same set of generic parameters: it lists the approximately 60 declarations so each implementation knows a specific type of each other implementation and substitute this type in method parameters and returns as appropriate. It appears that this (perhaps over)use of generics is causing the compiler to hang. The compiler throws an OutOfMemoryError, but does not appear to terminate.

考虑到我的情况,一个代码列表甚至包括这些声明之一是有点困难,但是一部分列表可能是可能的。接口声明通常指定约五个方法的集合,但在声明中使用泛型将源模块的大小增加到大约一千行。

Considering my circumstance, a code listing comprising even one of these declarations is a little difficult, but a part of a listing might be possible. The interface declarations commonly specify a collection of about five methods, but the use of generics in the declaration increases the size of a source module to about a thousand lines.

我的case是编译器实际上进入无限循环的情况,还是我应该给它更多的内存?抛出OutOfMemoryError异常大约需要20分钟,所以我担心,如果我只是通过一个因素增加了给编译器的内存,那么编译器抛出异常的时间会更长。

Could my case be a case of the compiler actually going into an infinite loop, or should I just give it more memory? It takes about 20 minutes for the OutOfMemoryError exception to be thrown, so I'm concerned that if I just increase the memory given to the compiler by a factor, it will just take that factor longer for the compiler to throw the exception.

我使用NetBeans作为我的编辑器环境,但是一旦我启动NetBeans,就一直在运行clean / build脚本。我这样做是因为NetBeans在启动后开始检查我的代码的语法时,迅速变得无响应。我使用Ubuntu 10.4(我想 - 我写这个从Windows)。我已经诉诸于在Ubuntu环境中打开命令行,运行NetBeans作为后台进程,检查输出并使用gedit修正任何源代码错误,杀死并重新启动NetBeans。这似乎足够了,直到我发现没有更多的语法错误正在产生。我不知道如何从命令行运行clean / build脚本。

I'm using NetBeans as my editor environment, but have been resorting to running the clean/build script as soon as I start NetBeans. I do this because NetBeans quickly becomes unresponsive when it begins to check the syntax of my code after start-up. I'm using Ubuntu 10.4 (I think - I'm writing this from Windows). I have resorted to opening a command line in the Ubuntu environment, running NetBeans as a background process, checking the output and correcting any source code mistakes using gedit, killing and then restarting NetBeans. This appeared to suffice until I found that no more syntax errors were being produced. I don't know how to run the clean/build script from the command line.

对不起,如果这个问题显得模糊,但如果其他人可以帮助我,那么也许我可以

Sorry if this question appears vague, but if others could help me then maybe I could be a little more specific.

感谢任何考虑过的建议。

Thanks for any considered advice.

推荐答案

p>这听起来像我已经有一些过于复杂的泛型使用,这导致Java编译器使用过量的内存,试图表示泛型类型的扩展。失败之前的很长时间可能是由于垃圾收集死亡螺旋... GC在这段时间里花费越来越多的时间,每次回收越来越少的内存,直到它终于必须放弃。

It sounds to me like you've got some overly complicated use of generics, and this is causing the Java compiler to use an excessive amount of memory trying to represent the expansions of the generic types. The very long times before failure are probably due to a "garbage collection death spiral" ... where the GC spends more and more time, reclaiming less and less memory each time, until it finally has to give up.

如果你不能向我们展示代码,唯一可能的建议是使用更大的堆来运行编译器,看看是否能解决这个问题。

If you can't show us the code, the only possible suggestion is to run the compiler with a larger heap, and see if that cures the problem.


我不知道如何从命令行运行clean / build脚本。

I don't know how to run the clean/build script from the command line.

好吧,我想你需要弄清楚。它可以帮助你解决问题,重要的是要知道这些东西反正;例如因此您可以使用CI系统来构建代码并运行测试。 (您可能需要从命令行运行该脚本,以使该构建在单独的JVM中运行,该堆的大小可以独立于IDE进行控制。)

Well, I think you need to figure that out. It may help you solve the problem, and it is important to know these things anyway; e.g. so that you can use a CI system to build your code and run your tests. (And you will probably need to run the script from the command line to get the build to run in a separate JVM with a heap whose size you can control independently of your IDE.)

复杂的泛型使用也可能包括Java编译器无法处理的病态(也可能是荒谬的)。

It is also possible that your complicated use of generics includes something pathological (and probably nonsensical) that the Java compiler cannot cope with.


我的泛型声明的一个可能的麻烦来源是他们是自我参照的;一个声明指的是另一个引用第一个。

A probable source of trouble about my generic declarations is that they're self referential; one declaration refers to another which refers to the first.

Yea ...那是我谈论的那种东西。如果你不小心,你可能会遇到无意义的(从Java角度来看)涉及无限类型扩展的东西。

Yea ... that's the kind of thing that I'm talking about. If you are not careful, you can end up with something that is meaningless (from a Java perspective) that involves infinite type expansions.

这篇关于使用Java泛型可以挂起编译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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