PC Review


Reply
Thread Tools Rate Thread

Is this a bug in the humble ListBox?!?

 
 
Ben
Guest
Posts: n/a
 
      9th Feb 2004
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


 
Reply With Quote
 
 
 
 
Klaus H. Probst
Guest
Posts: n/a
 
      9th Feb 2004
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.

--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/

"Ben" <(E-Mail Removed)> wrote in message
news:c07cer$o2j$(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      9th Feb 2004
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
>
> 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
>
>



 
Reply With Quote
 
Ben
Guest
Posts: n/a
 
      9th Feb 2004
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


"Cor" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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
> >
> > 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
> >
> >

>
>



 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      9th Feb 2004
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

> 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
>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      9th Feb 2004
Cor <(E-Mail Removed)> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The Humble Indie Bundle #3 Abarbarian Gaming 2 29th Jul 2011 12:41 AM
humble beginnings Tie Various Windows Vista General Discussion 3 30th Oct 2007 08:23 PM
Eating Humble Pie Legion Computer Hardware 1 13th Apr 2006 10:09 PM
Humble apology about Novell5x DubboPete Microsoft Access Form Coding 0 21st Jul 2004 03:18 PM
Keep interested in your own career, however humble. Graham Microsoft Excel Setup 0 11th Feb 2004 09:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:08 AM.