473,394 Members | 1,773 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,394 software developers and data experts.

Multithreading, DataTables, and Garbage Collection

Hi everyone,

I've got a console application that grabs data into 3 datatables
asynchronously, and writes each datatable's data to a textfile. The problem
is that the data returned by each sproc could easily hit up to 100,000
records each. Since the datatable doesn't implement IDisposable, would it
be prudent of me to set the datatable reference to null and immediately
force a garbage collection to release the memory used by the datatable? I
read somewhere this practice is discouraged - but the exe can consume up to
200 MB RAM unless I force the GC.

Here's some of my code. Any comments?. This is my first attempt at
multithreading so I can use all the help I can get!
Thanks,
Jenny
p.s. please respond to the group.

public class MainClass
private static void DoStuff{
/*create the delegates*/
MainClass.GetData1Delegate d1 = new GetData1Delegate (getData1);
MainClass.GetData2Delegate d2 = new GetData2Delegate (getData2);
MainClass.GetData3Delegate d3 = new GetData3Delegate (getData3);

//call the delegates and pass params and add 2 null params (callback and
asyncstate)
System.IAsyncResult ar1 = d1.BeginInvoke(dt1,batDate,null,null);
System.IAsyncResult ar2 = d2.BeginInvoke(dt2,batDate,null,null);
System.IAsyncResult ar3 = d3.BeginInvoke(dt3,batDate,null,null);

bool asyncsComplete = false;
bool d1Ended = false;
bool d2Ended = false;
bool d3Ended = false;

while(!asyncsComplete){ //Poll for completion every 1000 milliseconds
if(ar1.IsCompleted && !d1Ended){
d1.EndInvoke(ar1);
//write dt1 to text file
dt1 = null;
System.GC.Collect();
d1Ended = true;
}
if(ar2.IsCompleted && !d2Ended){
d2.EndInvoke(ar2);
//write dt2 to text file
dt2 = null;
System.GC.Collect();
d2Ended = true;
}
if(ar3.IsCompleted && !d3Ended){
d3.EndInvoke(ar3);
//write dt3 to text file
dt3 = null;
System.GC.Collect();
d3Ended = true;
}
asyncsComplete = (d1Ended && d2Ended && d3Ended);
Thread.Sleep(1000);
}

} //end DoStuff

/* Data Getters */
public delegate void GetData1Delegate(DataTable dt1, string batDate);
public static void GetData1(DataTable dt1, string batDate){
//run sproc and fill dt1
}

public delegate void GetData2Delegate(DataTable dt2, string batDate);
public static void GetData2(DataTable dt2, string batDate){
//run sproc and fill dt2
}

public delegate void GetData3Delegate(DataTable dt3, string batDate);
public static void GetData3(DataTable dt3, string batDate){
//run sproc and fill dt3
}
} //end class
Nov 13 '05 #1
2 4205
hmmm....no replies?
I guess I'm on my own on this one. :(

Regards,
J
"Jenny K" <je*****@hotmail.com> wrote in message
news:uU**************@tk2msftngp13.phx.gbl...
Hi everyone,

I've got a console application that grabs data into 3 datatables
asynchronously, and writes each datatable's data to a textfile. The problem is that the data returned by each sproc could easily hit up to 100,000
records each. Since the datatable doesn't implement IDisposable, would it
be prudent of me to set the datatable reference to null and immediately
force a garbage collection to release the memory used by the datatable? I
read somewhere this practice is discouraged - but the exe can consume up to 200 MB RAM unless I force the GC.

Here's some of my code. Any comments?. This is my first attempt at
multithreading so I can use all the help I can get!
Thanks,
Jenny
p.s. please respond to the group.

public class MainClass
private static void DoStuff{
/*create the delegates*/
MainClass.GetData1Delegate d1 = new GetData1Delegate (getData1);
MainClass.GetData2Delegate d2 = new GetData2Delegate (getData2);
MainClass.GetData3Delegate d3 = new GetData3Delegate (getData3);

//call the delegates and pass params and add 2 null params (callback and
asyncstate)
System.IAsyncResult ar1 = d1.BeginInvoke(dt1,batDate,null,null);
System.IAsyncResult ar2 = d2.BeginInvoke(dt2,batDate,null,null);
System.IAsyncResult ar3 = d3.BeginInvoke(dt3,batDate,null,null);

bool asyncsComplete = false;
bool d1Ended = false;
bool d2Ended = false;
bool d3Ended = false;

while(!asyncsComplete){ //Poll for completion every 1000 milliseconds
if(ar1.IsCompleted && !d1Ended){
d1.EndInvoke(ar1);
//write dt1 to text file
dt1 = null;
System.GC.Collect();
d1Ended = true;
}
if(ar2.IsCompleted && !d2Ended){
d2.EndInvoke(ar2);
//write dt2 to text file
dt2 = null;
System.GC.Collect();
d2Ended = true;
}
if(ar3.IsCompleted && !d3Ended){
d3.EndInvoke(ar3);
//write dt3 to text file
dt3 = null;
System.GC.Collect();
d3Ended = true;
}
asyncsComplete = (d1Ended && d2Ended && d3Ended);
Thread.Sleep(1000);
}

} //end DoStuff

/* Data Getters */
public delegate void GetData1Delegate(DataTable dt1, string batDate);
public static void GetData1(DataTable dt1, string batDate){
//run sproc and fill dt1
}

public delegate void GetData2Delegate(DataTable dt2, string batDate);
public static void GetData2(DataTable dt2, string batDate){
//run sproc and fill dt2
}

public delegate void GetData3Delegate(DataTable dt3, string batDate);
public static void GetData3(DataTable dt3, string batDate){
//run sproc and fill dt3
}
} //end class

Nov 13 '05 #2
I am just learning about this stuff myself so I hesitate to jump in.
Please take this post with a grain of salt. I am not an expert with
threading but I might be able to help a little.

It seems to me that you are doing a lot of work that doesn't need to
be done. The purpose of the callback is so that when there is work to
be done the method specified will be called, right? Here you are
firing up each event but then you are looping to monitor for the
results?

Rather than looping you should be able to use a ManualResetEvent this
doesn't consume the resources that looping does. It just puts the
calling thread (the one that started the async calls) in a
waiting/suspeded state. Then when one of the async calls is done you
can call Set() on the ManualResetEvent that will bring the calling
thread back to life. If you need to wait on all of the trheads before
bringing the main thread back to life I think that you can use the
ManualResetEvent for that too.

Some or all of this post may be wrong. Like I said I am not an expert
but perhaps what I have said above will help you find the right
answer. I would also recommend the book Inside C#. It has an excellent
chapter on threading and a sperate chapter on deligates. Very well
writing and easy to understand.

Best regards,
Ryan Pedersen

On Tue, 8 Jul 2003 10:57:03 -0700, "Jenny K" <je*****@hotmail.com>
wrote:
hmmm....no replies?
I guess I'm on my own on this one. :(

Regards,
J
"Jenny K" <je*****@hotmail.com> wrote in message
news:uU**************@tk2msftngp13.phx.gbl...
Hi everyone,

I've got a console application that grabs data into 3 datatables
asynchronously, and writes each datatable's data to a textfile. The

problem
is that the data returned by each sproc could easily hit up to 100,000
records each. Since the datatable doesn't implement IDisposable, would it
be prudent of me to set the datatable reference to null and immediately
force a garbage collection to release the memory used by the datatable? I
read somewhere this practice is discouraged - but the exe can consume up

to
200 MB RAM unless I force the GC.

Here's some of my code. Any comments?. This is my first attempt at
multithreading so I can use all the help I can get!
Thanks,
Jenny
p.s. please respond to the group.

public class MainClass
private static void DoStuff{
/*create the delegates*/
MainClass.GetData1Delegate d1 = new GetData1Delegate (getData1);
MainClass.GetData2Delegate d2 = new GetData2Delegate (getData2);
MainClass.GetData3Delegate d3 = new GetData3Delegate (getData3);

//call the delegates and pass params and add 2 null params (callback and
asyncstate)
System.IAsyncResult ar1 = d1.BeginInvoke(dt1,batDate,null,null);
System.IAsyncResult ar2 = d2.BeginInvoke(dt2,batDate,null,null);
System.IAsyncResult ar3 = d3.BeginInvoke(dt3,batDate,null,null);

bool asyncsComplete = false;
bool d1Ended = false;
bool d2Ended = false;
bool d3Ended = false;

while(!asyncsComplete){ //Poll for completion every 1000 milliseconds
if(ar1.IsCompleted && !d1Ended){
d1.EndInvoke(ar1);
//write dt1 to text file
dt1 = null;
System.GC.Collect();
d1Ended = true;
}
if(ar2.IsCompleted && !d2Ended){
d2.EndInvoke(ar2);
//write dt2 to text file
dt2 = null;
System.GC.Collect();
d2Ended = true;
}
if(ar3.IsCompleted && !d3Ended){
d3.EndInvoke(ar3);
//write dt3 to text file
dt3 = null;
System.GC.Collect();
d3Ended = true;
}
asyncsComplete = (d1Ended && d2Ended && d3Ended);
Thread.Sleep(1000);
}

} //end DoStuff

/* Data Getters */
public delegate void GetData1Delegate(DataTable dt1, string batDate);
public static void GetData1(DataTable dt1, string batDate){
//run sproc and fill dt1
}

public delegate void GetData2Delegate(DataTable dt2, string batDate);
public static void GetData2(DataTable dt2, string batDate){
//run sproc and fill dt2
}

public delegate void GetData3Delegate(DataTable dt3, string batDate);
public static void GetData3(DataTable dt3, string batDate){
//run sproc and fill dt3
}
} //end class

Mar 18 '06 #3

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

Similar topics

6
by: Ganesh | last post by:
Is there a utility by microsoft (or anyone) to force garbage collection in a process without have access to the process code. regards Ganesh
11
by: Rick | last post by:
Hi, My question is.. if Lisp, a 40 year old language supports garbage collection, why didn't the authors of C++ choose garbage collection for this language? Are there fundamental reasons behind...
34
by: Ville Voipio | last post by:
I would need to make some high-reliability software running on Linux in an embedded system. Performance (or lack of it) is not an issue, reliability is. The piece of software is rather simple,...
5
by: Bob lazarchik | last post by:
Hello: We are considering developing a time critical system in C#. Our tool used in Semiconductor production and we need to be able to take meaurements at precise 10.0 ms intervals( 1000...
20
by: Cybertof | last post by:
Hello, Is there a good way to call a big time-consumming function from an ActiveX DLL (interoped) through a thread without blocking the main UI ? Here are the details : I have a class...
2
by: SStory | last post by:
Here is the situation. I want to display Icons, Type of file etc from a file extension. Upon initial program load I may only need icons for certain files. But other operations will require...
8
by: mike2036 | last post by:
For some reason it appears that garbage collection is releasing an object that I'm still using. The object is declared in a module and instantiated within a class that is in turn instantiated by...
56
by: Johnny E. Jensen | last post by:
Hellow I'am not sure what to think about the Garbage Collector. I have a Class OutlookObject, It have two private variables. Private Microsoft.Office.Interop.Outlook.Application _Application =...
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.