使用mysql函数时Java总内存和内存使用量会增加吗?

时间:2012-08-20 05:58:35

标签: java mysql

我们有一段代码根据mysql定义的函数进行选择。我们已经发现,一旦启用了这部分,那么runtime.totalMemory()并且使用的内存不断增加。以下是代码片段。问题始于String selectQuery4。知道为什么这是一个问题?

double currentLong = Double.parseDouble(longitude);
double currentLat = Double.parseDouble(latitude); 

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

String selectQuery3 = "Select geoFenceID,enterpriseID,clientID,geoFenceString,geoFenceType,geoFenceName,geoFenceDescription,geoFencePreference, geoFenceRadius From tblGeoFence Where ((enterpriseID="+enterpriseID+" And clientID=0) Or (enterpriseID="+enterpriseID+" And clientID="+clientID+")) And geoFenceStatus='a'"; 
ResultSet rs3 = stmt6.executeQuery(selectQuery3);

while(rs3.next()) {
    geoFenceID = rs3.getInt("geoFenceID");
    geoFenceType = rs3.getString("geoFenceType");
    geoFenceString = rs3.getString("geoFenceString");
    geoFenceRadius = rs3.getInt("geoFenceRadius");
    geoFencePreference = rs3.getString("geoFencePreference");
    String polygon  =  "GeomFromText('"+geoFenceString+"')";
    String point    =  "GeomFromText('POINT("+currentLong+"  "+currentLat+")')";

    Statement stmt7 = null;
    stmt7 = dbconn.createStatement();
    String selectQuery4 = "SELECT GISWithin("+point+","+polygon+") As geoFenceStatus";
    ResultSet rs4 = stmt7.executeQuery(selectQuery4);

    if(rs4.next()) {

        if(rs4.getInt("geoFenceStatus")==1) {

            geoFenceIDFound=geoFenceID;
            geoFenceName = rs3.getString("geoFenceName");
            geoFenceDescription = rs3.getString("geoFenceDescription");                                                     
            geoFencePreference = rs3.getString("geoFencePreference");   

            if(lastGeoFenceID==geoFenceID) {
                geoFenceInID = geoFenceID;                  
                break;
            } else {

                if(previousGeoFenceID==geoFenceID &&(previousTimeDifferenceInt<0 || geoFenceArriveTimeDifferenceInt <= 0)) //check the funny problem double entry back into the geo fence
                {
                    lastGeoFenceID=geoFenceID;
                    geoFenceInID = geoFenceID;
                    break;
                } else {
                    geoFenceAlertEmailMessage="\nGeo Fence Alert Arrival\nTime:"+dateTimer+"\nGeo Fence Name :"+geoFenceName+"\nGeo Fence Description :"+geoFenceDescription+"\nGeo Fence Preference:"+geoFencePreference;

                    if(lastGeoFenceEntryStatus.equals("") || lastGeoFenceEntryStatus.equals("Out")) {
                        geoFenceEntryStatus="In";
                    } else {
                        geoFenceEntryStatus="Out";
                    }
                }
            }
        }
    }
}

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

用于打印内存统计信息的代码。

int mb = 1024*1024;
System.out.println("##### Heap utilization statistics [MB] #####");

//Print used memory
System.out.println("Used Memory:" + (runtime.totalMemory() - runtime.freeMemory()) / mb);

//Print free memory
System.out.println("Free Memory:" + runtime.freeMemory() / mb);

//Print total available memory
System.out.println("Total Memory:" + runtime.totalMemory() / mb);

//Print Maximum available memory
System.out.println("Max Memory:" + runtime.maxMemory() / mb);

1 个答案:

答案 0 :(得分:5)

您的代码应该在try / finally块中调用close。 您还应该在字符串中使用PreparedSTatements而不是参数concat。

关于您的问题,我们不确定您是否有问题:

  • 您的第一个while循环执行了多少次迭代?

  • 使用jconsole连接到你的jvm并在循环结束后调用garbage collect以查看内存是否下降:

  • 如果确实如此,那么你就没有真正的pb

  • 如果没有,那么你有

此致