Skip to main content

Posts

Showing posts with the label Memory

Performance and Memory tips

Two things play a very significant role in any application development, and those are application’s footprint and performance. Whenever I’m asked to work on these two tracks, I used to visit a huge list of sites to get many more ideas apart from what I already know. So, I thought, why can’t I collate all the good points and add them to my repository. At the same time, I thought of sharing those points in this blog. Rather than making this blog post full of theory, I’m planning to make it simple by just adding the bullet points. Uhh! Enough of gossip. Let’s get started by going through some common and important rules. Create object only when it is really required More the objects lesser the performance :(  Grab resources, use them and release at the earliest Default capacity of StringBuilder is 16. So, if you want to store less than 16 values then make sure to set the capacity. Avoid unnecessary boxing and unboxing Prefer lazy loading Use Static variables cautiously

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