Solved

Programming Practice adivce -- Tight Loops and New

Posted on 2006-10-24
4
251 Views
Last Modified: 2010-04-23
Is this code inherently bad?  
The datareader has about 30,000 rows.   At this point, the Cls1 and Cls2 classes perform no operation other than Get and Set values.     Ultimately SQL database updates will be done based upon Cls2Instance values.

The reason that I ask is that VS2005 truly yelled at me while running this in test mode.  I didn't write down the error at the time but, in essence, it had to do with MDAs and running dangerously close to being out of memory.    I watched the Task Manager while the app ran and, indeed, the available workstiaton memory shrunk at a good clip.  

These instantiations are done in a very tight loop.  But will loosen up after the complete code is in place.   I'm assuming that the garbage collector can't keep up right now.

So the questions:
1.  Should class instantiations be done like this?
2.  Is there a way to programmaticly (spelling) dispose of the Class at the end of each loop?
I could just variables and update them upon each loop iteration but.... that seems inelegant.   Each of the two classes has about 45 elements.

Thoughts?

Thank you.
 
           While dr.Read()
                Dim Cls1Instance As New Cls1
                With Cls1Instance
                     ' Assign Values
                End With

                Dim Cls2Instance As New Cls2
                With Cls2Instance
                      ' Assign Values from Cls1Instance
                End With

               ' Update SQL database with Cls2Instance values
            End While
0
Comment
Question by:RichardKline
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 4

Accepted Solution

by:
Xeavn earned 175 total points
ID: 17798546
What is dr.Read() here? It almost looks like you are reading out of a database, assigning them to a Cls1 Class, and then Taking all your values from Csl1 class and putting them in Cls2 class, and then reading them back into the database?

I guess I don't know enough from what little you posted to let you know if there is a better way to do this. Although one thought would be to created the Cls1 and Cls2 classes outside of the loop, and just reuse them each time, clearing them out after doing the database update. That should lower your memory usage.
0
 
LVL 8

Assisted Solution

by:bramsquad
bramsquad earned 75 total points
ID: 17798955
you can always do GC.Collect(); to force collection, but i dont think its needed in your example.

but like Xeavn said, initialize outside of the loop.  youll conserve a lot of memory that way.
0
 
LVL 1

Author Comment

by:RichardKline
ID: 17799583
Thanks!

It's an database update app.  Reading from a text file, doing some manipulation magic and then updating a database with new/modified records.

0
 
LVL 14

Expert Comment

by:ptakja
ID: 17799599
I agree with the other 2 guys. If you wrote Cls1 & Cls2, you could add a method called Clear or Reset or something that would essentially return the object to the same state as when it was created.

GC.Collect will force garbage collection. This would work, however, you have to be careful about how many times you call it as it can have a very negative effect on your app's performance.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net error: An exception occurred during a WebClient request 6 57
Access Schema 6 53
VB.net Is there code behind PowerPivot 2 66
Web page design problem 3 37
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question