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?