We’re writing a diagnostic tool that needs to run for many hours at a time, but we’re running into a mysterious Out of Memory Exception when we try to remove items from a CheckedListBox after the application has run for a couple of hours.
We initially tried using checkedListBox.Items.Clear();, and after some Googling around, we tried something like the following instead:
for (int i = checkedListBox.Items.Count - 1; i >= 0; i--)
{
checkedListBox.Items.RemoveAt(i);
}
Unfortunately, the above did not solve the issue. I found that idea on the MSDN forums, but I can’t for the life of me find the link again this morning. However, that forum did say that someone had profiled their application and found a memory leak in CheckedListBox.Items.Clear().
Does anyone know of a functional work around?
EDIT: FingerTheCat’s answer has temporarily solved our problem, so I will mark it as the answer for now. However, we’ve begun combing through the code to try and find the real problem. Unfortunately, the current implementation is largely spaghetti code, so it may be a few days before we find anything.
As it turns out, someone left a piece of debugging code in the application that was appending rather verbose log information to an
ArrayListwithout ever clearing it. That code was also appending copies of theArrayListto itself. Definitely not a good thing to forget to take out.