Monday, January 11, 2010

Memory leak updates

Turns out the chart in Netbeans was misleading.  I ran my program again as an independent jar file rather than through Netbeans, and the RAM usage was much more accurate.  I'm still working on it, but it mayyy be that the memory issue is fixed.

As part of my thinking on this, I also believe that part of the issue is the way I'm dealing with SwingWorker threads.  With everything, you learn ways of doing things as you work with something new.  I realized that the SwingWorker constructors in all my classes typically have some object that either stores or displays information.  For example, the DuplicatesTask class takes an ArrayList of file objects.  I use that object to store the duplicate files that are found, so that if the user cancels, the object is still updated with at least some duplicates.  I now see that is not the correct way to do things, I've learned more about SwingWorker listeners and I plan to switch over to a listener that publishes new duplicate files that are found.

I've also simplified the BackgroundMonitor idea.  Rather than search for new files and then fire an event with changed folders, why not just scan the music directory every time.  It doesn't really add much overhead and simplifies code.  On top of that, the actual AddTracksTask class is in the process of being reworked as well.  I came across an idea that should speed things up a little bit.  The new process is a variation of the old but faster.  Here it goes...

1.  Get all locations of files in iTunes (either through XML or iTunesController).
2.  Get all files in the music directory that agree with the MediaFileFilter.
3.  Read the exclusions list.
4.  Iterate over the iTunes track locations and remove those file paths from the music directory file paths.
5.  Iterate over the exclusions and remove those files from the music directory.
6.  Whatever is left are new add those to iTunes.

It should make the RAM usage a little less, as I'm using HashMaps, and if I decide to switch to the XML file, iTunes does not need to be open to check for new files.  I'm still debating on that one.

Anyway, dinner time.

No comments:

Post a Comment