0

We have a snippet of codes which does a select based on the mysql defined function.We have caught that once this portion is enabled then runtime.totalMemory() and used memory keep increasing. Below is the snippet of codes. The problem starts from String selectQuery4. Any idea why is this a problem?

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

Codes used to print the memory stats.

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);
4
  • Have you used a profiler like VisualVM to inspect the heap? Commented Aug 20, 2012 at 6:00
  • @philips I have added the codes which I used to print the statistics. In addition I have read before visualvm but the problem the machine is centos and a remote machine. We have done many tests with other codes and found this section is where it grows. Commented Aug 20, 2012 at 6:06
  • 2
    Is your process eventually running out of memory? The way you are looking at memory utilization is pointless in a managed memory environment. Have you tried analyzing what's happening with the garbage collector? Which generation is growing? Commented Aug 20, 2012 at 6:10
  • @Dmitry I am not too good with this tool but I have tried this /usr/java/jdk1.7.0_03/bin/jstat -gcutil 7525. I notice there is a change in the S0 and S1 values. So is that healthy or not? I need the right way of looking at the memory usage please guide then? Commented Aug 20, 2012 at 6:31

1 Answer 1

5

Your code should call close in try/finally blocks. You should also use PreparedSTatements instead of parameters concat in a string.

Regarding your question, it is not sure you have a problem:

  • How much iterations does your first while loop do ?

  • use jconsole to connect to your jvm and call garbage collect after the loop end to see if memory drops down:

  • if it does then you don't have a real pb

  • if it does not then you have

Regards

8
  • what happens is right at the top I have a try and below is catch where I capture for any rollbacks. So I notice if I keep the queries for e.g. stmt7.executeQuery(selectQuery4); in inner try and catch then I cant capture if there is any problem. The iteration can up to few thousand but now is just few hundreds. Can you guide me how to connect the jconsole to my jvm? I am a newbie to this tools. Commented Aug 20, 2012 at 6:37
  • See stackoverflow.com/questions/914775/… , it works with jdk>= 1.5 . Commented Aug 20, 2012 at 6:41
  • @are you talking about jmx is it ? what values to look into it? Commented Aug 20, 2012 at 9:48
  • Follow memory behaviour graph with jconsole and see if after your code when you call gc, memory drops down or not Commented Aug 20, 2012 at 10:17
  • I have now setup jstatd is running but from visuamvm I am not able to connect to my remote application. I have open all 3 port 1099,43588 and 20654. So what else must I do? Commented Aug 20, 2012 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.