We are doing a memory test of an application on centos. What we notice is that top shows the memory keep growing which we suspect due to the JVM requiring extra space. In addition in the application codes we have this few lines at the end. What we notice this also showing that the used memory and heap size also keep growing at first it was in tens of megabytess and now is in few hundreds of megabytess value.
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);
On the other hand we use this command /usr/java/jdk1.7.0_03/bin/jstat -gcutil 22805 and notice that after some time both the S0 and S1 is being cleaned to zero? Does this interpretation gives some indication of memory problem or this is normal behavior of java?
Below is the skeleton of our codes. From statement 6 I have a big loop of while then in it I got further many queries could this be the culprit?
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);
}
}
Sounds like your application has a memory leak. It is not normal for Java’s memory usage to increase at a constant rate when performing a process which doesn’t require more memory over time.