堆大小继续增长但jstat S0& S1继续清洁

时间:2012-08-19 06:12:50

标签: java

我们正在对centos上的应用程序进行内存测试。我们注意到顶部显示内存不断增长,我们怀疑这是因为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开始,我有一个很大的循环然后在其中我得到了更多的疑问,这可能是罪魁祸首吗?

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


     }

1 个答案:

答案 0 :(得分:1)

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