-2

so I made my own class-object for the date (DMY.java), and when I try to sort the list, the memory usage goes from 20,000k to almost 700,000k. I have narrowed it down to the sorting method that was causing the memory to go through the roof. It instantiates a few objects in the method, so I assume that is what is taking all of the memory up. But does anyone think that they can help me optimize it to use less memory? Here is the code:

   public static ArrayList<String> sortListByDate(ArrayList<String> list)
   {
       DMY currDate = null;
       DMY oldDate1 = null;
       DMY oldDate2 = null;
       ArrayList<String> temp = new ArrayList<String>();

       for(int x = 0; x < list.size(); x++) {
           currDate = new DMY(Regex.matchPattern(list.get(x), "\\d+[-]\\d+[-]\\d+ \\d+[:]\\d+[:]\\d+").trim(), false);

           if(temp.size() == 0) { 
               temp.add(list.get(x)); 
           } else if(temp.size() == 1) {
               oldDate1 = new DMY(Regex.matchPattern(temp.get(0), "\\d+[-]\\d+[-]\\d+ \\d+[:]\\d+[:]\\d+").trim(), false);
               if(currDate.compareToFull(oldDate1) == -1) {
                   temp.add(0, list.get(x));
               } else {
                   temp.add(list.get(x));
               }
           } else {
               for(int y = 0; y < temp.size(); y++) {
                   if(!(x + 1 < temp.size())) {
                       temp.add(list.get(x));
                   } else {
                       oldDate1 = new DMY(Regex.matchPattern(temp.get(x), "\\d+[-]\\d+[-]\\d+ \\d+[:]\\d+[:]\\d+").trim(), false);
                       oldDate2 = new DMY(Regex.matchPattern(temp.get(x + 1), "\\d+[-]\\d+[-]\\d+ \\d+[:]\\d+[:]\\d+").trim(), false);

                       if(currDate.compareToFull(oldDate1) == 1 && currDate.compareToFull(oldDate2) == -1) {
                           temp.add(x + 1, list.get(x));
                       } 
                   }
               }
           }       
       }

       return temp;
   }

Basically, if the size of the sorted list is 0, just add the line to the new list. If the size of the sorted list is 1, see if the new date is before or after that one date, then add it to the sorted list accordingly. Otherwise, simply loop through each value in the list and see if the date of the next two lines are newer or older than the current one. Then add it accordingly.

As you can see, there are a lot of objects and a lot of loops. The original list is a list of strings that contain a date. What the method does is get the date using regex and then compares it to the other dates in the list. For some reason it is using a lot of memory and I need to reduce it. Any ideas?

Zach the Dev
  • 1
  • 1
  • 5
  • 6
    Why not define a custom `Comparator` and use `Collections.sort()`? – GriffeyDog Jun 18 '14 at 15:02
  • Because the arraylist of the string is not only the date. It has other information on it as well. So wouldn't the sort call sort it by something else? – Zach the Dev Jun 18 '14 at 15:03
  • Your `Comparator` can define the sort criteria any way you want. – GriffeyDog Jun 18 '14 at 15:04
  • http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property – cwin Jun 18 '14 at 15:05
  • If the date is in the format "dd/mm/yyyy hh:mm:ss" how would I compare it? Can you give me an example? – Zach the Dev Jun 18 '14 at 15:06
  • 1
    Use `SimpleDateFormat` to convert `String`s to `Date`s, then you can use `Date.compareTo()`. – GriffeyDog Jun 18 '14 at 15:08
  • does it mean you hold a whole date as a string? why? operations on strings are much more taxing than operations on numbers. same goes for using regular expressions. – kacpr Jun 18 '14 at 15:09

1 Answers1

0
  • Make use of Collections.sort instead of rolling your own
  • Use a Comparator to implement the desired sorting behavior
  • Refrain from using expensive operations that implicitly create Objects (e.g. RegEx, String.trim()). If you can't do without RegEx, at least precompile and reuse them.
  • Generally avoid object creation just for the sake of comparing
Durandal
  • 19,919
  • 4
  • 36
  • 70