I'm working on a Windows Service under C# which basically scans a specific directory for newly created files. If a file has been created, the filename string is split and a specific substring of the filename used to query a SQL database. As this service is running permanently i want it to be as memory friendly as possible and therefore I've been told to use the automatic garbage collection while putting the SqlDataAdapter and the DataTable objects in usings. Take the following code into consideration:
using (SqlDataAdapter da = new SqlDataAdapter(cmdstring, con))
{
using (DataTable dt = new DataTable())
{
da.Fill(dt);
if (dt.Rows.Count != 0)
{
splitlst.Add(dt.Rows[0][0].ToString());//kunde
splitlst.Add(dt.Rows[0][1].ToString());
splitlst.Add(dt.Rows[0][2].ToString());
splitlst.Add(dt.Rows[0][3].ToString());
splitlst.Add(dt.Rows[0][4].ToString());
splitlst.Add(dt.Rows[0][5].ToString());
}
}
}
Is this an efficient way to handle the garbage collection? I'm not really used to working this way, so if anyone could make me understand when to deliberately control the garbage collection, i would be very greatful.