Correct Usage of Collections?

G

Guest

I'm currently working on a report to forecast production for finished goods.
The user can select one or more items to forecast. In addition, they may
select one or more warehouses to view breakdowns for as well as one or more
customers.

Now, the report iterates through the selected items in the finished good
listbox. For each item that it comes across, it calls 4 routines to gather
various totals. Each routine will gather a total for the item as well as
totals for each selected warehouse and customer.

So, for example, a routine could possibly read 100 records that match a
specific item. A total will be calculated for all 100 records for the
finished good item. Let's say, of those 100 records, 25 are for warehouse A,
25 for warehouse B, 25 for C and 25 for D. Of that 100, 50 are for Customer
1 and 50 for Customer 2. To help keep track of the warehouse and customer
totals, I decided to use collections. That way, I could gather the totals,
hold them in the collections and retrieve them by a key (the warehouse letter
or customer number).

Here's the problem. When the program runs, just before the user clicks the
Print button, the memory usage is at around 25MB. As the program runs, the
memory usage climbs at a steady pace. The highest I've seen it go is close
to 180MB when the users select all criteria for the report.

I've used collections in other languages before but I'm new to .NET
collections. In this report, I am using the Microsoft.VisualBasic.Collection
collection.

First, I've noticed that you can only Add or Remove items; you cannot edit
entries in the collection. To add each quantity that I come across, I have
to retrieve the value for the warehouse/customer, add the quantity to it,
remove the entry from the collection and add the new value to the collection.

Second, I've noticed that there is no Clear() method on this type of
collection. To clear the collection, I have to iterate through the items in
the collection and remove each entry. I assume that, with the memory usage
climbing, memory is not freed when I remove an entry and replace it with
another.

I would like to keep using collections because it is so easy to retrieve the
entries by a key value. However, the memory usage is bothering me. I've
tried setting the collections to Nothing and reinstantiating them but the
memory usage still climbs.

Am I using the collections correctly? Is there another type of collection
that will allow me to Clear() the entries? Even if there is another type,
would the memory usage drop?

Any help would be greatly appreciated.
 
R

Richard Myers

Hi Tim

You haven't told us what your collecting? i.e What object type?
VB.Net is object oriented so you can easily add your own methods for this
by simply extending the collectionBase base class.
Depending on object type i would be looking at implementing the IDisposable
interface. Clearing the collection is all well and good but it sounds like
your not correctly disposing of your objects.

Object editing cannot be accomplished via For Each iteration but can via
For i=0 to (mycollection.count-1) iteration.

This all sounds a little messy. Are you sure you cant calculate this info
using some intermediate SQL? Sounds like a classic Group By (Totals) to me.

Richard
 

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