Skip to main content

Posts

Showing posts with the label Garbage collector

Performance analysis for String and StringBuilder

Sometimes small-small changes in our code really makes a huge difference to a performance. There are many tips and tricks available and among those, one I am going to discuss over here. I'll be talking about  String vs StringBuilder. One needs to be very careful while playing with strings because memory wise there is a huge impact of strings. I  know, there are lots and lots of articles available on net on String and StringBuilder, but still I am going to show this, using some statistics. Here I am taking Fx 4.0 C# console application with different static methods to showcase my analysis.  Basically what I am doing here is, I am having a String variable named  outputString  and just looping that for 1000 times and concating the string to variable outputString.  Please note, concatenation is done using + symbol. So, what happens internally is, whenever concatenation is done using + symbol, every time, new String object is created. So, as with my snippet. Here I am  lo

Finalize in .Net

We implement the Finalize method to release the unmanaged resources. First let’s see, what is managed and unmanaged resources. Managed ones are those, which we write in .Net languages. But when we write code in any non .Net language like VB 6 or any windows API, we call it as unmanaged. And there are very high chances that we use any win API or any COM component in our application. So, as managed resources are not managed by CLR, we need to handle them at our own. So, once we are done with unmanaged resources, we need to clean them. The cleanup and releasing of unmanaged is done in Finalize(). If your class is not using any unmanaged resources, then you can forget about Finalize(). But problem is, we can’t directly call Finalize(), we do not have control of it. Then who is going to call this. Basically GC calls this. And one more thing to remember is, there is no Finalize keyword that we will write and implement it. We can define Finalize by defining the Destructor. Destructor is us