Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.OutOfMemoryException: java heap space

Tags:

java

While running the following code i am getting the error java.lang.OutOfMemoryException : java heap space

My code is:

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);
 }
}

I am using NetBeans IDE... Is there any option there to control this kind of errors?

like image 963
RAVITEJA SATYAVADA Avatar asked Jul 08 '26 01:07

RAVITEJA SATYAVADA


1 Answers

If you go on increasing without knowing the cause, there is a possibility that your problem might not be solved.So I suggest you to find the root cause of the problem and solve it from there,

These are some of the free tools which can be used to analyze heap and will help you to get out of OutOfMemoryError :

  • Visualgc : Visualgc stands for Visual Garbage Collection Monitoring Tool and you can attach it to your instrumented hostspot JVM. Main strength of visualgc is that it displays all key data graphically including class loader, garbage collection and JVM compiler performance data. The target JVM is identified by its virtual machine identifier also called as vmid.

  • Jmap : Jmap is a command line utility comes with JDK6 and allows you to take a memory dump of heap in a file. It’s easy to use as shown below:

    jmap -dump:format=b,file=heapdump 6054

    Here file specifies name of memory dump file which is "heapdump" and 6054 is PID of your Java progress. You can find the PDI by using "ps -ef” or windows task manager or by using tool called "jps"(Java Virtual Machine Process Status Tool).

  • Jhat : Jhat was earlier known as hat (heap analyzer tool) but it is now part of JDK6. You can use jhat to analyze heap dump file created by using "jmap". Jhat is also a command line utility and you can run it from cmd window as shown below:

    jhat -J-Xmx256m heapdump

    Here it will analyze memory-dump contained in file "heapdump". When you start jhat it will read this heap dump file and then start listening on http port, just point your browser into port where jhat is listening by default 7000 and then you can start analyzing objects present in heap dump.

  • Eclipse memory analyzer : Eclipse memory analyzer (MAT) is a tool from eclipse foundation to analyze java heap dump. It helps to find classloader leaks and memory leaks and helps to minimize memory consumption.you can use MAT to analyze heap dump carrying millions of object and it also helps you to extract suspect of memory leak.

  • VisualVM : VisualVM is a visual tool integrating several commandline JDK tools and lightweight profiling capabilities. Designed for both production and development time use, it further enhances the capability of monitoring and performance analysis for the Java SE platform.

  • YourKit

Courtesy : solution of java.lang.OutOfMemoryError in Java

like image 165
COD3BOY Avatar answered Jul 10 '26 15:07

COD3BOY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!