在运行以下代码时,我收到错误java.lang.OutOfMemoryException : java heap space
我的代码是:
public class openofficeupdate {
String databaseurl="C:\\mydbdir\\location\\salesforce"; // Path of the base after renaming and extraction
openofficeupdate() throws ClassNotFoundException, SQLException{
System.out.println("Entered into constructor");
Connection connection=null;
Statement statement=null;
try{
Class c=openofficeclass();
System.out.println("Class name set");
Connection cntn=createConnection(databaseurl);
connection=cntn;
System.out.println("connection created");
Statement stmt=createStatement(cntn);
statement=stmt;
System.out.println("Statement created");
executeQueries(stmt);
System.out.println("Query executed");
closeStatement(stmt);
System.out.println("Statement closed");
closeConnection(cntn);
System.out.println("Connection closed");
}catch(Exception e){
System.out.println(e);
closeStatement(statement);
System.out.println("Statement closed");
closeConnection(connection);
System.out.println("Connection closed");
}
}
public static void main(String args[]) throws ClassNotFoundException, SQLException{
new openofficeupdate();
}
private Class openofficeclass() throws ClassNotFoundException {
return Class.forName("org.hsqldb.jdbcDriver");
}
private Connection createConnection(String databaseurl) throws SQLException{
return DriverManager.getConnection("jdbc:hsqldb:file:" +databaseurl,"sa","");
}
private Statement createStatement(Connection cntn) throws SQLException{
return cntn.createStatement();
}
private void closeStatement(Statement stmt) throws SQLException{
stmt.close();
}
private void closeConnection(Connection cntn) throws SQLException{
cntn.close();
}
private void executeQueries(Statement stmt) throws SQLException{
System.out.println("Going to execute query");
int status=stmt.executeUpdate("insert into \"Mobiles\" values(9874343210,123,'08:30:00','09:30:06')");
12','2010-12-14','c','Casula')");
System.out.println("Query executed with status "+status);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 NetBeans IDE... 是否有任何选项可以控制此类错误?
如果你不知道原因就继续增加,你的问题有可能得不到解决,所以我建议你找到问题的根本原因并从那里解决,
这些是一些可以用来analyze heap帮助您摆脱困境的免费工具OutOfMemoryError:
Visualgc:Visualgc 代表 Visual Garbage Collection Monitoring Tool,您可以将其附加到您的检测主机点 JVM。visualgc 的主要优势在于它以图形方式显示所有关键数据,包括类加载器、垃圾收集和 JVM 编译器性能数据。目标 JVM 由其虚拟机标识符标识,也称为vmid。
Jmap:Jmap 是 JDK6 附带的命令行实用程序,它允许您将堆的内存转储到文件中。使用起来很简单,如下图所示:
jmap -dump:format=b,file=heapdump 6054
这里 file 指定内存转储文件的名称,即“heapdump”,6054 是您的 Java 进度的 PID。您可以使用“ps -ef”或 Windows 任务管理器或使用名为“jps”(Java 虚拟机进程状态工具)的工具找到 PDI。
Jhat:Jhat 早先被称为 hat(堆分析器工具),但现在它是 JDK6 的一部分。您可以使用 jhat 来分析使用“jmap”创建的堆转储文件。Jhat 也是一个命令行实用程序,您可以从 cmd 窗口运行它,如下所示:
jhat -J-Xmx256m heapdump
在这里它将分析包含在文件“heapdump”中的内存转储。当您启动 jhat 时,它将读取此堆转储文件,然后开始侦听 http 端口,只需将浏览器指向 jhat 默认侦听的端口 7000,然后您就可以开始分析堆转储中存在的对象。
Eclipse 内存分析器:Eclipse 内存分析器 (MAT) 是 Eclipse 基金会的一个工具,用于分析 Java 堆转储。它有助于发现类加载器泄漏和内存泄漏,并有助于最大限度地减少内存消耗。您可以使用 MAT 分析携带数百万个对象的堆转储,它还可以帮助您提取内存泄漏的嫌疑人。
VisualVM:VisualVM 是一个可视化工具,集成了多个命令行 JDK 工具和轻量级分析功能。专为生产和开发时使用而设计,它进一步增强了 Java SE 平台的监控和性能分析能力。
礼貌:Java 中 java.lang.OutOfMemoryError 的解决方案