Showing posts with label Garbage collector. Show all posts
Showing posts with label Garbage collector. Show all posts

March 14, 2011

ASP.NET Performance Tip3 - Use using key word

The using keyword is basically a try - finally block, without a catch , where IDisposable.Dispose() is called in the finally .

Using block defines a scope, outside of which an object or objects will be disposed.

Use using keyword for the objects that  hold any resources other than managed memory.  For Example like files, sockets, database connections, or even GDI drawing handles.

We should  be using "using" whenever the class implements IDisposable. So use of using keyword will always help to avoid Garbage Collection call or explicitly calling garbage collector to

destroy objects.

For example

using(Dataset ds = new Dataset())
{
//code
}

We do not need to worry about destroying objects once the job is done. Garbage Collector will take care of it.


using(ClassName x = new ClassName())
{

     ...use x here...

}

This will automatically remove the resources occupied by the obejct x.

Another Example while working with database objects :

 

using ( SqlConnection connection = new SqlConnection(connectionString) )


{

      SqlCommand cmd = new SqlCommand(commandString, connection);


      connection.Open();


      using (SqlDataReader reader = cmd.ExecuteReader())


      {


         while (reader.Read())


         {

    listBox1.Items.Add(reader[0].ToString() + ", " + reader[1].ToString());
}
}
}
But normally we use try ,catch and dispose objects explicitly in Finally. Why to do this when we have the job that will be done by CLR through GC with the help of using keyword. A "using" statement can be exited either when the end of the "using" statement is reached or if an exception is thrown and control leaves the statement block before the end of the statement. Multiple Objects can be used with using statement as
using (Furniture chair = new Furniture(300, 400),
table = new Furniture(20, 100))
{
    // Use chair and table.
}

It is very important to know when to use what and implement best practices to achieve for performance , reliability

June 17, 2006

Identifying Memory Leaks in Managed code

Q: Can you have memory leaks in managed code? If so, how can you catch them?

A: Garbage Collector will reclaim objects at its own pace, based on balancing available memory and runtime overhead. If an assembly is terminated and it is known to contain a significant percentage of total set of managed objects, then it is best practice to call GC.Collect() after the unload.

If we define a leak, as the memory usage grows over time, then it is possible to have leaks in a GC runtime. For example if you keep ever growing data in a static variable , an editor keeps an infinite undo list, then this can be be considered as a leak.

As far as leak detection and diagnosis, you should look at the performance counters in the category. In .NET CLR Memory, You will find a lot of interesting counters like #object in all heaps. They will tell you if you are leaking objects.

We should check out the various CLR memory profilers out there. by examinining them you can get a picture of the heap and by comparing the pictures at 2 different points you can determine which objects are accumulating, then find out which code path allocates them in the first place, or even, which connected graph keeps them alive.