0

我们正在对 centos 上的应用程序进行内存测试。我们注意到,top 显示内存不断增长,我们怀疑这是由于 JVM 需要额外空间。此外,在应用程序代码中,我们最后有这几行。我们注意到这也表明使用的内存和堆大小也在不断增长,最初是几十兆字节,现在是几百兆字节的值。

Runtime runtime = Runtime.getRuntime();
long memory = runtime.totalMemory() - runtime.freeMemory();
System.out.println("\n\nUsed memory is bytes: " + memory);
//Print the jvm heap size.
long heapSize = runtime.totalMemory();
System.out.println("\n\nHeap Size = " + heapSize);

另一方面,我们使用此命令 /usr/java/jdk1.7.0_03/bin/jstat -gcutil 22805 并注意到一段时间后 S0 和 S1 都被清除为零?这种解释是否给出了一些内存问题的迹象,或者这是 java 的正常行为?

下面是我们代码的骨架。从语句 6 开始,我有一个很大的 while 循环,然后在其中我得到了更多的查询,这可能是罪魁祸首吗?

try{

         try{ //inner try for each complete message read from the socket connection

//lots of inner queries run based on some logics of if else etc


//sample query of opening and closing both statement and resultsets.

Statement stmt1 = null;
stmt1 = dbconn.createStatement();
String selectQuery1  = "Select query";
ResultSet rs1 = stmt1 .executeQuery(selectQuery1);
while(rs1.next()) {
//process here

}
try{
  if (rs1 != null ){
        rs1.close();
  }
  if (stmt1!= null ){
        stmt1.close()
  }
}
catch(SQLException ex){
  ex.printStackTrace(System.out);
}

          Statement stmt6 = null;
          stmt6 = dbconn.createStatement(); 

String selectQuery3 = "Select query....";
ResultSet rs3 = stmt6.executeQuery(selectQuery3);

          while(rs3.next()) {
              var1= rs3.getInt("value1");
    var2 = rs3.getString("value2");
    if(var2.equals("POINT"){
                  Statement stmt7 = null;
                  stmt7 = dbconn.createStatement();
        String selectQuery4= "select query ... ";
        ResultSet rs4 = stmt7.executeQuery(selectQuery4);
        if(rs4.next()){
    Statement stmt8 = null;
                        stmt8 = dbconn.createStatement();
                        if(lastID>0){

                             String updateQuery2 = "update query .......";
                             count = stmt8.executeUpdate(updateQuery2);
                             String insertQuery2 = "insert query .....";
                             count = stmt8.executeUpdate(insertQuery2);
                             String selectLast1 = "SELECT last_insert_id()";
         ResultSet rs5 = stmt8.executeQuery(selectLast1);
         if(rs5.next()){
                 gFAlertIDOut=rs5.getInt(1);
         gFOutID = lGFID;
         } 
                             String insertQuery3 = "insert query .....";
                             count = stmt8.executeUpdate(insertQuery3);
                             String selectLast2 = "SELECT last_insert_id()";
         rs5 = stmt8.executeQuery(selectLast2);
         if(rs5.next()){
                 gFAlertIDIn=rs5.getInt(1);
         gFIntID = lGFID;
         } 
                             try{
                       if (rs5 != null ){
                           rs5.close();
                      }
                      if (stmt8!= null ){
                         stmt8.close();
                      }
                  }
                  catch(SQLException ex){
                   ex.printStackTrace(System.out);
                  }   

                        }
                        else{

                       }                                 
                  }
                  try{
            if (rs4 != null ){
                rs4.close();
        }
        if (stmt7!= null ){
               stmt7.close();
        }
       }
       catch(SQLException ex){
          ex.printStackTrace(System.out);
       }   

              }
              else{
                  Statement stmt7 = null;
                  stmt7 = dbconn.createStatement();
        String selectQuery4= "select query ... ";
        ResultSet rs4 = stmt7.executeQuery(selectQuery4);
        if(rs4.next()){



                  }

                  try{
            if (rs4 != null ){
                rs4.close();
        }
        if (stmt7!= null ){
               stmt7.close();
        }
       }
       catch(SQLException ex){
          ex.printStackTrace(System.out);
       }   

              }


          }
          try{
    if (rs3 != null ){
       rs3.close();
    }
    if (stmt6!= null ){
       stmt6.close();
    }
}
catch(SQLException ex){
      ex.printStackTrace(System.out);
 }   

dbconn.commit();
}
catch (SQLException ex) { 
  try {    
    dbconn.rollback();  
  } 
  catch (Exception rollback){    
    rollback.printStackTrace(System.out);
  } 
}
catch (Exception e){
 try{    
        dbconn.rollback();  
  } 
  catch (Exception rollback) {    
    rollback.printStackTrace(System.out);
  }
}
     }
     catch (SocketTimeoutException ex){ 
           ex.printStackTrace();
     }  
     catch (IOException ex) { 
           ex.printStackTrace();
     }  
     catch (Exception ex){  
                    ex.printStackTrace(System.out);
     }      
     finally{
        try {
    if ( dbconn != null ){ 
       dbconn.close();
                        }
        }
        catch(SQLException ex){
                    ex.printStackTrace();
        }
        try{ 
              if ( writer1 != null ){
        writer1.close();
    }

        }
        catch(IOException ex){
           ex.printStackTrace(System.out);
        }


     }
4

1 回答 1

1

听起来您的应用程序存在内存泄漏。当执行一个不需要更多内存的进程时,Java 的内存使用量以恒定速率增加是不正常的。

于 2012-08-19T06:16:17.680 回答