Is this a bug in the humble ListBox?!?

B

Ben

Hi,

I seem to be having a memory management issue with the ListBox control.
Using .NET Memory Profiler, the Clear() method does not appear to actually
remove references to objects in the list box immediately. I have been able
to work around this by clearing the box, adding a dummy string entry, and
then clearing it again.

Here is some code to demonstrate this:

private class TestObj
{
public override string ToString()
{
return "Testing";
}
}

private void btnAdd_Click(object sender, System.EventArgs e)
{
listBox1.Items.Add(new TestObj());
}

// This does not appear to work!
private void btnClear_Click(object sender, System.EventArgs e)
{
listBox1.Items.Clear();
GC.Collect();
}

private void btnReset_Click(object sender, System.EventArgs e)
{
listBox1.Items.Clear();
listBox1.Items.Add("");
listBox1.Items.Clear();
GC.Collect();
}

Is this a bug in the ListBox, or is it by design?

Ben
 
K

Klaus H. Probst

Seems to me you're seeing the GC in action (or inaction). I don't think it's
a bug at all. Why do you need the objects to be disposed? They will be,
eventually. Just remove them from the list.
 
C

Cor

Hi Ben,

In addition to Klaus,

You want your computer only to be busy with collection the garbadge it
looks.
Why do you do that, that instruction is normaly not needed.
But it will need some time of course.

Cor
 
B

Ben

Hi,

I suppose I assumed that the GC would work instantly. The GC.Collect() calls
were there to force the GC to collect garbage (which these un-referenced
objects are) however if this takes time it is understandable that they would
not be collected straight away. Any idea roughly how long a complete GC
collection lasts? The reason I am using GC.Collect() is because my app
handles large amounts of data (probably roughly 30MB) and I obviously do not
want this lingering in memory for too long if it is not needed (for example
if another document is opened). It is quite possible that 30MB files will be
loaded in quick succession (perhaps 30 seconds each) and this could add up -
this is why I am concerned that the GC works efficiently.

Thanks,

Ben
 
C

Cor

Hi Ben,

I am not sure of the when the GC works, but it would be in an efficient way,
so I asume at the moment there is no activity on the system.

I think they are not that stupid at Microsoft to do it in a time when the
load is 95%

Also all memory activities I have seen in this newsgroups became all times
to no results,

One was very nice. Someone found a method to get the load in the Taskmanager
very low. Someone else proved that that was not the real amount and than the
other one said, "That does not matter, my system administrator is happy
now".

One of the reasons of the managed code is to managed the behaviour from the
memory. Why would I do that extra.

While I have never seen here that someone wrote that it is totally wrong,

Cor
 
J

Jon Skeet [C# MVP]

Cor said:
I think they are not that stupid at Microsoft to do it in a time when the
load is 95%

It will if it absolutely needs to (or if it's been told to) - the
alternative is to throw an exception saying you've run out of memory,
basically. I don't know how the GC is tuned in terms of *background*
collection though.
Also all memory activities I have seen in this newsgroups became all times
to no results,

Yup.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top