Friday, October 16, 2009

I was hacking something up for a proof-of-concept demo, and pulled in a bunch of code that did stuff that I needed. It was pretty old code, from around 2003 or so. I thought it was still being used, but now I'm not so sure. When I first tried using it, it threw a ConcurrentModificationException. I was only running one thread. I looked at the code and saw something like this:

for (Enumeration e = list.elements(); e.hasMoreElements(); ) {
Object element = e.nextElement();
...
// might as well remove that to clean up the memory and make the list smaller ...
list.remove(element);

The lists would never be big either, usually no more than 2 or 3 elements, and probably never more than 5 or so. The elements in the list wouldn't be big either, generally being strings of less than 10 characters and probably never more than 100 characters. The entire list would probably get garbage collected at the same time as the elements in the list anyhow. That list.remove() caused problems and the motivation for doing it was silly.

No comments:

Post a Comment