RAVITEJA SATYAVADA
RAVITEJA SATYAVADA

Reputation: 2571

java.lang.OutOfMemoryException: java heap space

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?

Upvotes: 2

Views: 8452

Answers (4)

COD3BOY
COD3BOY

Reputation: 12112

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

Upvotes: 5

stivlo
stivlo

Reputation: 85546

In Netbeans 7.0:

  • You can right click on the project, and select Properties.

  • Click on Run category and insert you configuration in VM Options

  • For instance, in your case, paste: -Xmx512m (512Mb) as suggested by rm5248

You can read Playing with JVM / Java Heap Size for further information.

Upvotes: 3

jayunit100
jayunit100

Reputation: 17650

If you run out of heap space then you need to increase your heap size. The way to know how much memory you use is by running approproiate tests and profiling. That said, there are some simpler ways to get your app up and running... Here are the 3 common things that people do to fix or address memory issues with java programs.

  • You can pass -Xmx1028 argument as in the previous post. This increases the maximum memory.
  • If you want to start off with a large memory footprint, you can optionally also pass -Xms1028.
  • You can pass -XX:+AggressiveHeap if you dont know before hand what your memory requirements will be. This is not the "official" best way to do things, but I find it always works quite well when trying to run a new application which I'm not yet sure of the memory requirements.

Upvotes: 1

rm5248
rm5248

Reputation: 2645

Pass -Xmx512m to the JVM as an argument. That will increase your maximum heap size to 512 MB. See http://download.oracle.com/javase/7/docs/technotes/tools/windows/java.html for more information on how this works.

As for how to set it in Netbeans, I'm not sure; if I remember correctly there's a setting for when you run the program for arguments to pass the program, as well as arguments for the JVM.

Upvotes: 0

Related Questions