简体   繁体   English

java中的内存泄漏?

[英]Memory Leakage in java?

The code below throws a java:out of memory exception.下面的代码抛出一个 java:out of memory 异常。 I have also seen that this Question has been answered many times here.我也看到这个问题已经在这里回答了很多次。 Seeing the previous solutions I also tried看到以前的解决方案我也尝试过

java -Xmx1024M YourClass. java -Xmx1024M 你的课堂。

I kept increasing 1024M.我一直在增加1024M。 I increased to the extent of 10240M, but still the same exception was thrown and additionally my system hanged for a few minutes.我增加到10240M的程度,但仍然抛出相同的异常,另外我的系统挂了几分钟。 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;一个 int 是 4 个字节; a reference is 4 bytes.一个引用是 4 个字节。

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.如果您的应用程序必须拥有所有数据,而您只有有限的 RAM,那么您就无法挑战物理学。 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.在该 catch 块中打印堆栈跟踪。 You're getting less information doing it your way.按照您的方式,您获得的信息较少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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