473,395 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Is this a bug in the humble ListBox?!?

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
Jul 21 '05 #1
5 1258
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" <b.*******@no.spam.carisco.co.uk> wrote in message
news:c0**********@news8.svr.pol.co.uk...
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

Jul 21 '05 #2
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

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

Jul 21 '05 #3
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
"Cor" <no*@non.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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


Jul 21 '05 #4
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
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

Jul 21 '05 #5
Cor <no*@non.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: amber | last post by:
Hello. Can someone tell me what I may be doing wrong here? I'm using the code (lboxRP is a listbox): Dim newRPindex As Integer newRPindex = Me.lboxRP.FindString(RP)...
3
by: Paul T. Rong | last post by:
I have a listbox (of product names) control on my form. I want to pass the selected item (a product name) to a subform, and the product unitprice should apear automatically next to the product name...
8
by: Oddball | last post by:
Ok - I have a ListBox control and I'm ready to write my own DrawItem event handler. What I want to draw as the item is another control. I have created a user control that I would like to list in...
6
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from another table. So far, I have been keeping the dataset...
7
by: Dave | last post by:
Hi all, After unsuccessfully trying to make my own dual listbox control out of arraylists, I decided to look for a 3rd party control. I've looked for over a week now and can't find anything but...
5
by: Ben | last post by:
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...
3
by: Ali Chambers | last post by:
Hi, I have created a listbox called "dtlist1" on my VB.NET form. I call a procedure as follows: Private Sub openfile(flname As String) dtlist1.Items.Clear() etc..
1
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which...
5
by: Academia | last post by:
(If you've seen this in the drawing NG, sorry. I inadvertently sent it there.) I have a listbox populated with Objects. The Class has a String field that ToString returns. I assume that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.