Java 程序意外终止,没有任何错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2189974/
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
Java program terminates unexpectedly without any error message
提问by Fahim
I have written a java program which needs to process thousands of text files (all needs to be loaded on memory). It works fine with as many as 123 input files, but when I run it to process around 5000 files, it terminates unexpectedly in the middle of the road, without giving any error message/exception. Can anyone give me clue about what might have gone wrong?
我编写了一个需要处理数千个文本文件的 java 程序(所有文件都需要加载到内存中)。它可以处理多达 123 个输入文件,但当我运行它来处理大约 5000 个文件时,它意外地在中间终止,没有给出任何错误消息/异常。谁能告诉我可能出了什么问题?
I am using jdk1.6 on Mac OS Leopard having 2GB RAM.
我在具有 2GB RAM 的 Mac OS Leopard 上使用 jdk1.6。
采纳答案by Stephen C
Given that it is your program, I suggest that you do the following:
鉴于这是您的程序,我建议您执行以下操作:
First, change the main
method so that everything is done in a try/catch block that reports all uncaught exceptions; e.g. something like this:
首先,更改main
方法,以便在报告所有未捕获异常的 try/catch 块中完成所有操作;例如这样的事情:
public static void main(String[] arghhhhh) {
try {
...
} catch (Throwable ex) {
System.err.println("Uncaught exception - " + ex.getMessage());
ex.printStackTrace(System.err);
}
}
Second, look for anywhere that you might "squash" unexpected exceptions by catching them and not reporting them.
其次,寻找可以通过捕获而不是报告它们来“压制”意外异常的任何地方。
Third, look for anywhere that you might call System.exit()
silently. This could happen in libraries too ... if you are using a badly written one.
第三,寻找您可能会System.exit()
默默呼叫的任何地方。这也可能发生在库中……如果您使用的是写得不好的库。
If those measures don't give you answers, try to figure HOW the application is exiting by
如果这些措施没有给你答案,试着弄清楚应用程序是如何退出的
- by running from a debugger with breakpoints set at key points, or
- by adding trace print statements at key points.
- 通过在关键点设置断点的调试器运行,或
- 通过在关键点添加跟踪打印语句。
回答by Andy White
Are you opening files simultaneously? You might be running out of memory if you are loading too many files at once. If the files are large enough, you might be running out of memory with only a single file open. Also, make sure you are closing the files when you're done with them.
你是同时打开文件吗?如果您一次加载太多文件,您可能会耗尽内存。如果文件足够大,您可能会因为只打开一个文件而耗尽内存。此外,请确保在完成文件后关闭文件。
回答by Vinodh Ramasubramanian
Check if you have any try/catch blocks that do not log exceptions properly.
检查您是否有任何未正确记录异常的 try/catch 块。
It could most likely be OutofmemoryError. Make sure the console isn't redirected.
它很可能是 OutofmemoryError。确保控制台没有被重定向。
回答by Upul Bandara
It seems like you are getting OutofmemoryError
.
看起来你正在得到OutofmemoryError
.
if it is the case try to increase heap memory size.
如果是这种情况,请尝试增加堆内存大小。
java -Xms<initial heap size> -Xmx<maximum heap size>
回答by Robert H?glund
There are mainly two reasons.
主要有两个原因。
- A "unhandled" system fault has occurred i.e. java.lang.OutOfMemoryError.
- A "unhandled" application fault has occurred.
- System.exit has been called.
- 发生了“未处理的”系统故障,即 java.lang.OutOfMemoryError。
- 发生了“未处理的”应用程序错误。
- System.exit 已被调用。
To deal with these scenarios consider to do the following steps:
要处理这些情况,请考虑执行以下步骤:
- Look in you code for calls to System.exit.
Ensure that you handles all exceptions in the starting Java stack frame, i.e. Main-method:
try{ ..code }catch(Throwable t){ t.printStackTrace }
Ensure that you have control where the stdout and stderr is directed. You may programatically set these to concrete files:
- 查看调用 System.exit 的代码。
确保您处理了起始 Java 堆栈帧中的所有异常,即 Main-method:
try{ ..code }catch(Throwable t){ t.printStackTrace }
确保您可以控制 stdout 和 stderr 的指向。您可以以编程方式将这些设置为具体文件:
System.setOut(new PrintStream("output.txt")); System.setErr(new PrintStream("err.txt"));
System.setOut(new PrintStream("output.txt")); System.setErr(new PrintStream("err.txt"));
- Start with args as: -Xloggc:gc.log -XX:+PrintGCDetails -XX:+PrintGCTimeStamps
- 从参数开始: -Xloggc:gc.log -XX:+PrintGCDetails -XX:+PrintGCTimeStamps