Mondblut
Mondblut

Reputation: 329

When is it advisable to control the garbage collection under C#

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.

Upvotes: 0

Views: 279

Answers (1)

Flater
Flater

Reputation: 13813

The code is good; but for the wrong reasons (sort of).


I've been told to use the automatic garbage collection

Somewhat facetiously, you don't need to explicitly use the automatic garbage collection. Because it's automatic.


using blocks are used for disposing objects (which implement the IDisposable interface).

The intention (lowering memory usage) is the same here. Many disposable objects free their data; so that the automatic garbage collector is allowed to clean it when it wants to.


As a very minor comment, you can reduce nesting by omitting the brackets of the top using statement:

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());
    }
}

Similar to other code blocks (if, foreach, ...), the braces are not necessary for a single line of code.

Technically, you'd still need to indent. Similar to doing:

if(1 + 1 != 2) 
    BurnMathBooks(); //still indented

You should technically also do:

using (SqlDataAdapter da = new SqlDataAdapter(cmdstring, con))
    using (DataTable dt = new DataTable())  //still indented
    {
        //...
    }

But since the lack of indentation doesn't actually make the code more unreadable; it's acceptable to omit it in this case.

using (SqlDataAdapter da = new SqlDataAdapter(cmdstring, con))
using (DataTable dt = new DataTable())
{
    //...
}

Upvotes: 1

Related Questions