Why is this static method slowly taking longer?

I have a static method that I use to process ResultSets

and return an object back. Sometimes it can be called over and over again. For example, in the current project I am working on, the static method is called over 56,000 times. For whatever reason, when it comes up over and over again, the method takes longer and takes longer to respond.

I used System.currentTimeMillis()

to figure out where in the code it keeps getting longer, and while I know it's currentTimeMillis()

not 100% accurate, the time it takes to complete this piece of code will slowly and exponentially increase over time.

Here is the code:

public static Object createFromResultSet(ResultSet someResultSet, String someIndex) throws SQLException, Exception {
    { ... }
    List<Object[]> result = new ArrayList();
    List<String> columnNames = new ArrayList();
    List<Object> addRow;

    // the code below will take longer and longer to complete as the method is called over and over again
    while (someResultSet.next()) {
        addRow = new ArrayList();
        for (int x = 0; x < columnNames.size(); x++) {
            if (someResultSet.getObject(x + 1) instanceof Double) {
                Double someValue = someResultSet.getDouble(x + 1);
                addRow.add(new BigDecimal(someValue).toPlainString());
            }
            else if (someResultSet.getObject(x + 1) != null) addRow.add(someResultSet.getObject(x + 1).toString());
            else addRow.add("");
        }
        result.add(addRow.toArray());
    } 
    { ... }
}

      

I may not fully understand how static methods work in Java, but I think that all the variables in this method will only be used once per time the static method is called (i.e. if the method is called again, there are no values ​​carried over from the previous time the method was called). Apart from the variables that are passed, static variables that were declared outside the method are not used. However, the method gets slower and slower over time, making me think there is data that is being used in previous calls to this method. The 56,000 times it is called in my current project can take up to an hour to run this report.

Something tells me that I have something wrong in my logic, but for the likes of me I don't know what's going on.

+3


source to share


1 answer


It just happens because the memory is completely loaded. You have to do 3 things:



  • Make them global. This way, there will be no GC call for every method exit. The memory will be allocated only once, and there will be no move-divorce.

    List<Object[]> result;
    List<String> columnNames;
    List<Object> addRow;
    
          

  • At the end of the createFromResultSet method, clear these lists. For example:.

    addRow.clear();
    
          

  • Create "addRow = new ArrayList ();" in place of the declaration itself.

    List<Object> addRow = new ArrayList();
    
          

-1


source







All Articles