Sunday, May 27, 2012

Split multi page tiff file - (C# code attached)

While working with image files, one of the biggest constraint is the size of file. When file is too big, then it need too much time to process add load. So, to resolve this issue, we can split one big image (tiff) file into various pages. This code sample will explain how to work with TIFF (Tagged Image File Format) using c#.net. It will cover splitting  a multipage tiff file into multiple tiff files and reading the properties of tiff file using c#. TIFF files are one of the format in which images can be saved. To split multipage tiff file, mainly 3 steps are required as 1) Get the total number of pages in a TIFF file 2) Get encoder information for the TIFF type file 3) Save each page of multipage  TIFF file into seperate TIFF files. In TIFF, there are predefined types, which tell the value of an item is of which type...


For more details, please visit my post titled 'Split multi page tiff file' at MSDN.

Sunday, May 20, 2012

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 looping 1000 times, so, it is creating 1000 String objects and every time is is replaced with variable outputString . That way, whenever we use string concatenation with the plus (+) sign, it is definitely going to cost our application performance.

Well, I guess this much boring theory is enough. Let's move towards statistics. 

Here I am using CLR Profiler and is really one of the good tool to analyse our code performance. This tool tells us, how much memory bytes are consumed, Garbage Collector  performance and how many objects it is moving to generation Gen0, Gen1 and Gen2 buckets. And at the same time statistics provided by this tool is very easy to understand.

Ok, I just ran CLR Profiler for the above code and got the below statistics. Here I am not going to cover GC generations in detail, but would like to touch bit on it. One must know that all the objects created in application, first comes to G0 bucket and then older objects are moved to G1 bucket. If the G1 bucket is going to full then older objects get moved to G2 bucket.  But for .Net GC, frequency of visiting G1 and G2 is very less, compare to the G0 bucket. It means that GC is visiting bucket 0 frequently, so it is releasing G0 objects much frequently and the scope of object is also very less. So, if your application is creating objects which lot many objects are moving to G1 and G2, then it is not a good sign. 

Now quickly jumping back to our example:

Here we see that heap bytes are present in all three Gen 0,Gen 1,Gen 2 and even the memory wise also it is 7 digit (2,894,353).
 Here Relocated bytes means it is going to be the part of G1 related objects. Here I am not going to analyse all the result, but somehow we are seeing here some negative signs because few of the objects are falling in G1 and G2 buckets also.

Now before commenting on it, lets take StringBuilder's data. In this example, I just created a StringBuilder instance named sb. Here I am doing the same thing, but instaed of string, I am taking instance of StringBuilder. In case of StringBuilder, whenever value will be appended, it will not create any new object but just updates the reference of the sb object with the new value. So, internally it is not creating a new object for every concatenation. So, this is the real benefit of StringBuilder as compare to String object.

Although we are looping for 1000 times, but it doesn't mean that we are creating 1000 string objects. That's the way we are controlling memory usage and creation of new objects. Now will run profiler and checkout the results.



Here we see that memory bytes are reduced to 5 digits (92, 332) and relocated bytes are nothing. If we will see that Heap bytes, it is unknown (0) for all G0, G1 and G2. It means, none of the objects are moved to G1 and G2. All the objects are created in G0 and release from G0 itself.



So, here we noticed that there is a significant difference in both memory usage as well as GC's bucket movements.

Hence we can conclude that we should prefer to use StringBuilder, rather than String specially when  we are dealing with concatenations. 

Friday, May 18, 2012

BackgroundWorker in .Net Console Application


Today I was just doing net surf and came across one interesting question 'Can progress event of BackgroundWorker execute after completed event'. At first I thought no, but when I tried this with Console application, I was also able to reproduce this issue. Now question is, how come this scenario occurs in Console app and not in Windows form. Pretty interesting, right ?


Now coming to Windows form, this issue will never occur, due to message queuing support. Windows message queue takes very good care of execution sequence of the events. This clearly mean that the progress event may run after the DoWork has completed, but the completion event will always happen afterwards. Another interesting thing here is the SynchronizationContext, which helps in maintaining all these sequencing.


But when talking about Console application, none of the above holds true. There is no SynchronizationContext installed and the events just end up in getting run in threadpool thread, which doesn't guarantee any order.


Test case: I created a console app and used Backgroundworker with all the required event handlers. In progress event handler, I added below lines:
Console.WriteLine("One");
Console.WriteLine("Two");
Console.WriteLine("Three");
Console.WriteLine("Four");


On executing the console application, I found that output messages are not in the order, which I mentioned in code. On standard output I received Two, Three, Four, One and sometimes I received One, Two, Three, Four and sometime, I also found one of the message missing and in output I got only Two, Three, Four. But in Windows Form, I always get the output in correct order as One, Two, Three, Four.


I hope, above analogy makes sense.

Resource name can not be used more than once

Recently I came across an error "Resource name can not be used more then once". Apart from this, error message was not showing any other information, not even line number, file name, nothing. Generally such errors came, when there is any duplicate key present in resource file, but in my case, I was not using any resource file also. So, there is no chance of duplicate keys also. I tried to hit my head many times for some online help, but no luck :(


One thing I noticed was, after building my solution (it was in VS2010) for 3-4 times continuously, error was thrown. Please note, I was just building the solution, without doing any modification in my code or in any of the files. Still I didn't get any clue.


So, finally I thought to remove one one project from my solution and build. Till 4-5 projects I removed and I didn't get any clue till yet. Suddenly I found that, Obj folder is added to my solution explorer. This obj folder holds all temporary files with few .resource files. Then I realize that, entire issue was due to this Obj folder, because whenever we build our solution, Visual Studio tries to create some files, and in my case these files were already part of that Obj folder.


Till now also, I am not sure, how that Obj folder get added to my solution explorer. Probably by mistake, I might have clicked on "include in project", as "Show all files" option was also enabled.


But finally, I was able to figure out the cause and thought to share it here. 
Hope it will help you !!!



Saturday, May 12, 2012

Matching braces in code

In day-to-day life developers use to write huge logic involving many braces ({,}) in the code. Reaching to end/start of any condition gets complex as the lines of code increases. To simplify the same, one can use key combination of Ctrl+].


To use the given key combination, place the cursor on any brace and hit Ctrl+]. If the brace is an end brace, the control will move to the matching brace i.e. start brace of the condition and vice versa.


Also the same key combination can be used to navigate to the matching comment (/*, */) or region (#region). In these cases, the cursor position should be on the comment or the region respectively.


Hope this helps !!!