简体   繁体   中英

Memory Leakage in java?

The code below throws a java:out of memory exception. I have also seen that this Question has been answered many times here. Seeing the previous solutions I also tried

java -Xmx1024M YourClass.

I kept increasing 1024M. I increased to the extent of 10240M, but still the same exception was thrown and additionally my system hanged for a few minutes. Is there any other solution to this problem?

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.Array;
import java.sql.SQLException;
import java.util.Collection;


 class PostgreSQLJDBC 
 {
    public static void main( String args[] ) throws SQLException {
    Connection c = null;
    Statement stmt = null;
    Statement stmt1 = null;  
    int way=1367184;
    int nodecount=7045400;
    int count=0;
    int idarr[]=new int[1367184];
    Long node[][]=new Long[1367184][];
    Long child[][]=new Long[7045400][2000];
    int nodearray[]=new int[7045400];
    int i=0;
    try 
    {
       Class.forName("org.postgresql.Driver");
       c = DriverManager
          .getConnection("jdbc:postgresql://localhost:5432/pr",
          "postgres", "password");
       c.setAutoCommit(true);
       stmt = c.createStatement();
       stmt1 = c.createStatement();
       ResultSet rs = stmt.executeQuery( "SELECT * FROM planet_osm_ways;" );
       ResultSet rs1 = stmt1.executeQuery( "SELECT * FROM 
                                           planet_osm_nodes;");
       while ( rs.next() )
       {
          idarr[i] = rs.getInt("id");
          Array array=rs.getArray("nodes");
          node[i]=(Long[])array.getArray();
          i++;
       }
       i=0;
       /*while(rs1.next())
       {
          nodearray[i]=rs1.getInt("id");
          i++;
       }*/
       rs.close();
       stmt.close();
       c.close();
     } 
    catch ( Exception e ) 
    {
      System.err.println( e.getClass().getName()+": "+ e.getMessage() );
      System.exit(0);
    }

 }

}

Could Someone please help?

An int is 4 bytes; a reference is 4 bytes.

Multiply out all the memory you're allocating and add up the bytes. It's a simple calculation.

If your application has to have all the data, and you only have a limited amount of RAM, then you can't defy physics. You have to rethink what you're doing.

This is bad code, for many reasons.

Encapsulate that data into an object and use a List. Only bring what you need back to the server.

Print the stack trace in that catch block. You're getting less information doing it your way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM