Harsha projects

Yet another Tech blog !

Archive for the ‘C Sharp’ Category

Locking critical section by an object used by multiplethreads

leave a comment »

Yesterday, In one of the discussion, there was an interesting question about threading.

What is the expected result if you have multiple thread running and each creates a new object but when these threads go through a critical section which is locked by a static object. As we all know it will take more time because of the static object used for synchronization. And its time would be more than if application runs without creating threads.

I was curious to see how much would be the difference –Time Difference.

First let me share my code:

namespace ThreadingCriticalSection
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Main thread");

            for (int i = 0; i <= 5; i++)
            {
                new ClassWithCriticalSection().LetsProcess();
            }

            Console.WriteLine("With 6 Thread");
            for (int i = 0; i <= 5; i++)
            {
                System.Threading.ThreadStart start = new System.Threading.ThreadStart(new ClassWithCriticalSection().LetsProcessWithThread);
                System.Threading.Thread th = new System.Threading.Thread(start);
                th.Start();
            }
            int j = 0;
            j++;           

        }
    }

    class ClassWithCriticalSection
    {
        public static object LockingObject = new object();

        public void LetsProcessWithThread()
        {
            lock (LockingObject)
            {
                LetsProcess();
            }
        }

        public void LetsProcess()
        {
            var entryTime = DateTime.Now;
            FindPrimeNumber(100000);
            var exitTime = DateTime.Now;
            var diff = exitTime.Subtract(entryTime).TotalMilliseconds;
            Console.WriteLine("Thread ID:{0}; {1},", System.Threading.Thread.CurrentThread.ManagedThreadId,diff.ToString());
        }

        public long FindPrimeNumber(int n)
        {

            int count = 0;
            long a = 2;
            while (count < n)
            {
                long b = 2;
                int prime = 1;// to check if found a prime
                while (b * b <= a)                 {                     if (a % b == 0)                     {                         prime = 0;                         break;                     }                     b++;                 }                 if (prime > 0)
                {
                    count++;
                }
                a++;
            }
            return (--a);
        }
    }

As you see, first I am trying to run the code 6 times without any threads and then I have created 6 threads which will go through the same code as before but with addition to locking code.

and final result:

threadingtime_graph

Time in Milliseconds. Time taken for calculation by main thread is little less compared time taken by threads.

So final question? how slow. In this case its 185.1111 Milliseconds. So lets do some math here:

when using Main thread, for an average processing time of 1066 millisecond, multi thread(6) might take 1099 milliseconds when static object is used for synchronization.

I have used the FindPrimenumber method from the the following  url and answered by Parag Meshram:

http://stackoverflow.com/questions/13001578/i-need-a-slow-c-sharp-function

Let me know your thoughts. Thanks for visiting my blog.

Written by Harsha

February 15, 2017 at 5:01 PM

Posted in C Sharp, Threading

WPF Performance Suite

leave a comment »

While reading more about the Rendercapability Tier, I bump into couple article where they mentioned about the WPF Performance Suite which comes with Windows SDK but I couldn’t find the Perforator tool alone.

Just lucky to find the link at the following site:
https://blogs.msdn.microsoft.com/kirillosenkov/2016/04/05/where-to-download-wpf-performance-suite-perforator-visual-profiler/

Direct link:
First install:
https://download.microsoft.com/download/A/6/A/A6AC035D-DA3F-4F0C-ADA4-37C8E5D34E3D/setup/WinSDKPerformanceToolKit_amd64/wpt_x64.msi

and then patch:
https://download.microsoft.com/download/1/8/9/189A7832-49D8-4978-85E8-3DFFF44E6C04/WpfPerf_timezone_patch.msp
When the patch installer prompts, choose “Repair”.
This WPF Performance Suite has two Tools: Perforator and Visual Profiler.

More details:
https://blogs.msdn.microsoft.com/jgoldb/2008/09/26/updated-wpfperf-performance-profiling-tools-for-wpf/

 

Written by Harsha

February 4, 2017 at 10:46 PM

Posted in C Sharp, WPF

WPF Rendering Tier

leave a comment »

As we all know WPF uses DirectX for rendering through MILCore. Rendering Tier will tell us whether Hardware accelaration is in use.

i.e,

int renderingTier =
(System.Windows.Media.RenderCapability.Tier >> 16)

renderingTier can be 0, 1, 2.

more details are here:
https://msdn.microsoft.com/en-us/library/system.windows.media.rendercapability.tier(v=vs.110).aspx

but based on the rederingTier, one can set RnderMode

HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
HwndTarget hwndTarget = hwndSource.CompositionTarget;
hwndTarget.RenderMode = RenderMode.SoftwareOnly;

 

but one has to set RenderMode in every window and there is no solution to set to entire application at once.

but from .net 4.0 we can set
RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;

OnStartup method in Application which will set to entire application at once.

So in .net 4.0 or above


using System.Windows;
using System.Windows.Media;

namespace RenderCapabilityDemo
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            if (ForceSoftwareRender)
            {
                RenderOptions.ProcessRenderMode =        System.Windows.Interop.RenderMode.SoftwareOnly;
            }
        }

        private bool ForceSoftwareRender
        {
            get
            {
                return (System.Windows.Media.RenderCapability.Tier >> 16) ==0 ;
            }
        }
    }
}

Written by Harsha

January 31, 2017 at 10:23 PM

Posted in C Sharp

CLR MD — Analyzing Live Process

with 2 comments

As we all know, there are many ways to analyze the application dump and also Live process. Most of us already using commercially available profilers for Live application analysis. We also have library (relatively new one ) CLRMD. Library is available with examples on GitHub.

Library is in Prerelease so when you adding thru the nuget manager please select Prerelease while searching.

InstallCLRMD

NuGet Package

 

Once you have installed, lib can be used by adding following namespace:

 using Microsoft.Diagnostics.Runtime;  

 

Important part is getting the heap.

 var process = Process.GetProcessesByName("ImageApplication").FirstOrDefault();  
       if (process != null)  
       {  
         int pid = process.Id;  
         const int timeout = 5000;  
         using (var dataTarget = DataTarget.AttachToProcess(pid, timeout))  
         {  
           var clrVersion = dataTarget.ClrVersions.FirstOrDefault();  
           string dacLocation = dataTarget.SymbolLocator.FindBinary(clrVersion.DacInfo);  
           ClrInfo version = dataTarget.ClrVersions[0];  
           ClrRuntime runtime = version.CreateRuntime(dacLocation);  
           var heap = runtime.GetHeap();  
         }  

 

ClrAndDacLocation

CLR Info, DAC and Heap

here is an example which pulls out all the objects a simple application(ImageApplication).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Diagnostics.Runtime;
using System.Diagnostics;
using System.Collections;

namespace SimpleAnalyzer
{
    class Program
    {
        static void Main(string[] args)
        {

            var process = Process.GetProcessesByName("ImageApplication").FirstOrDefault();

            if (process != null)
            {
                int pid = process.Id;
                const int timeout = 5000;

                using (var dataTarget = DataTarget.AttachToProcess(pid, timeout))
                {
                    var clrVersion = dataTarget.ClrVersions.FirstOrDefault();
                  
                    string dacLocation = dataTarget.SymbolLocator.FindBinary(clrVersion.DacInfo);

                    ClrInfo version = dataTarget.ClrVersions[0];

                    ClrRuntime runtime = version.CreateRuntime(dacLocation);
                    var heap = runtime.GetHeap();

                
                    ObjectSet liveObjs = null;
                    liveObjs = GetLiveObjects(heap);

                    Dictionary<ClrType, Entry> stats = new Dictionary<ClrType, Entry>();

                    Console.WriteLine("{0,16} {1,12} {2}", "Object", "Size", "Type");

                    foreach (ClrSegment seg in heap.Segments)
                    {
                        for (ulong obj = seg.FirstObject; obj != 0; obj = seg.NextObject(obj))
                        {
                            if (!liveObjs.Contains(obj))
                                continue;

                            // This gets the type of the object.
                            ClrType type = heap.GetObjectType(obj);
                            ulong size = type.GetSize(obj);

                            Console.WriteLine("{0,16:X} {1,12:n0} {2}", obj, size, type.Name);

                            // Add an entry to the dictionary, if one doesn't already exist.
                            Entry entry = null;
                            if (!stats.TryGetValue(type, out entry))
                            {
                                entry = new Entry();
                                entry.Name = type.Name;
                                stats[type] = entry;
                            }

                            // Update the statistics for this object.
                            entry.Count++;
                            entry.Size += type.GetSize(obj);
                        }
                    }

                    // Now print out statistics.

                    Console.WriteLine("Stats");

                    // We'll actually let linq do the heavy lifting.
                    var sortedStats = from entry in stats.Values
                                      orderby entry.Size
                                      select entry;

                    ulong totalSize = 0, totalCount = 0;
                    Console.WriteLine("{0,12} {1,12} {2}", "Size", "Count", "Type");
                    foreach (var entry in sortedStats)
                    {
                        Console.WriteLine("{0,12:n0} {1,12:n0} {2}", entry.Size, entry.Count, entry.Name);
                        totalSize += entry.Size;
                        totalCount += (uint)entry.Count;
                    }

                    Console.WriteLine();
                    Console.WriteLine("Total: {0:n0} bytes in {1:n0} objects", totalSize, totalCount);
                }
            }
            else
                Console.WriteLine("Process not found!");

            Console.ReadLine();


        }
        private static ObjectSet GetLiveObjects(ClrHeap heap)
        {
            ObjectSet considered = new ObjectSet(heap);
            Stack<ulong> eval = new Stack<ulong>();

            foreach (var root in heap.EnumerateRoots())
                eval.Push(root.Object);

            while (eval.Count > 0)
            {
                ulong obj = eval.Pop();
                if (considered.Contains(obj))
                    continue;

                considered.Add(obj);

                var type = heap.GetObjectType(obj);
                if (type == null)  // Only if heap corruption
                    continue;

                type.EnumerateRefsOfObject(obj, delegate(ulong child, int offset)
                {
                    if (child != 0 && !considered.Contains(child))
                        eval.Push(child);
                });
            }

            return considered;
        }

    }
    
    class DumpDiffEntry
    {
        public ClrType Type;
        public int Count;
        public long Size;
    }

    class Entry
    {
        public string Name;
        public int Count;
        public ulong Size;
    }
    class ObjectSet
    {
        struct Entry
        {
            public ulong High;
            public ulong Low;
            public int Index;
        }

        public ObjectSet(ClrHeap heap)
        {
            m_shift = IntPtr.Size == 4 ? 3 : 4;
            int count = heap.Segments.Count;

            m_data = new BitArray[count];
            m_entries = new Entry[count];
#if DEBUG
            ulong last = 0;
#endif

            for (int i = 0; i < count; ++i)
            {
                var seg = heap.Segments[i];
#if DEBUG
                Debug.Assert(last < seg.Start);
                last = seg.Start;
#endif

                m_data[i] = new BitArray(GetBitOffset(seg.Length));
                m_entries[i].Low = seg.Start;
                m_entries[i].High = seg.End;
                m_entries[i].Index = i;
            }
        }

        public void Add(ulong value)
        {
            if (value == 0)
            {
                m_zero = true;
                return;
            }

            int index = GetIndex(value);
            if (index == -1)
                return;

            int offset = GetBitOffset(value - m_entries[index].Low);

            m_data[index].Set(offset, true);
        }

        public bool Contains(ulong value)
        {
            if (value == 0)
                return m_zero;


            int index = GetIndex(value);
            if (index == -1)
                return false;

            int offset = GetBitOffset(value - m_entries[index].Low);

            return m_data[index][offset];
        }

        public int Count
        {
            get
            {
                // todo, this is nasty.
                int count = 0;
                foreach (var set in m_data)
                    foreach (bool bit in set)
                        if (bit) count++;

                return count;
            }
        }

        private int GetBitOffset(ulong offset)
        {
            Debug.Assert(offset < int.MaxValue);
            return GetBitOffset((int)offset);
        }

        private int GetBitOffset(int offset)
        {
            return offset >> m_shift;
        }

        private int GetIndex(ulong value)
        {
            int low = 0;
            int high = m_entries.Length - 1;

            while (low <= high)
            {
                int mid = (low + high) >> 1;
                if (value < m_entries[mid].Low)
                    high = mid - 1;
                else if (value > m_entries[mid].High)
                    low = mid + 1;
                else
                    return mid;
            }

            // Outside of the heap.
            return -1;
        }

        BitArray[] m_data;
        Entry[] m_entries;
        int m_shift;
        bool m_zero;
    }

}

 

Out Put is

          Object         Size Type
         26F1024           84 System.Exception
         26F1078           84 System.OutOfMemoryException
         26F10CC           84 System.StackOverflowException
         26F1120           84 System.ExecutionEngineException
         26F1174           84 System.Threading.ThreadAbortException
         26F11C8           84 System.Threading.ThreadAbortException
         26F121C           12 System.Object
         26F1228           14 System.String
         26F1238           28 System.SharedStatics
         26F1254          152 System.String
         26F12EC          206 System.String
         26F13BC          112 System.AppDomain
         26F142C           22 System.String
         26F1444           78 System.String
         26F1494           68 System.AppDomainSetup
         26F1598           28 System.String
         26F15C4           54 System.String
         26F15FC           88 System.String[]
         26F1730           28 System.String
         26F174C           16 System.String
         26F175C           38 System.String
         26F1784           44 System.String
         26F17B0           46 System.String
         26F17E0           34 System.String
         26F1804           28 System.String
         26F1820           30 System.String
         26F1840           38 System.String
         26F1868           58 System.String
         26F18A4           58 System.String
         26F18E0           64 System.String
         26F1920           52 System.String
         26F1954           50 System.String
         26F1988           44 System.String
         26F19B4           44 System.String
         26F19E0           30 System.String
         26F1A00           42 System.String
         26F1A2C           56 System.String
         26F1A64           36 System.String
         26F1A88           24 System.String
         26F1BA0           78 System.String
         26F1BF0           70 System.String
         26F1C38           82 System.String
         26F1C8C           36 System.Security.PermissionSet
         26F1CB0           20 Microsoft.Win32.SafeHandles.SafePEFileHandle
         26F1CC4           32 System.Security.Policy.PEFileEvidenceFactory
         26F1CE4           40 System.Security.Policy.Evidence
         26F1D0C           48 System.Collections.Generic.Dictionary<System.Type,System.Security.Policy.EvidenceTypeDescriptor>
         26F1D3C           28 System.RuntimeType
         26F1D58           28 System.RuntimeType
         26F1D74           28 System.RuntimeType
         26F1D90           28 System.RuntimeType
         26F1DAC           28 System.RuntimeType
         26F1DC8           28 System.RuntimeType
         26F1DE4           28 System.RuntimeType
         26F1E00           28 System.RuntimeType
         26F1E1C           28 System.RuntimeType
         26F1E38           28 System.RuntimeType
         26F1E54           12 System.Collections.Generic.ObjectEqualityComparer<System.Type>
         26F1E60           56 System.Type[]
         26F1E98           28 System.RuntimeType
         26F1EB4           28 System.RuntimeType
         26F1ED0           28 System.RuntimeType
         26F1EEC           28 System.RuntimeType
         26F1F08           28 System.RuntimeType
         26F1F24           28 System.RuntimeType
         26F1F40           28 System.RuntimeType
         26F1F5C           28 System.RuntimeType
         26F1F78           28 System.RuntimeType
         26F1F94           28 System.RuntimeType
         26F1FB0          300 System.Int32[]
         26F20DC           12 System.Object
         26F21E0           80 System.Int32[]
         26F2230          284 System.Collections.Generic.Dictionary+Entry<System.Type,System.Security.Policy.EvidenceTypeDescriptor>[]
         26F234C           44 System.Threading.ReaderWriterLock
         26F2378           28 System.Reflection.RuntimeAssembly
         26F2394           16 System.Security.Policy.AssemblyEvidenceFactory
         26F23C0           16 System.String[]
         26F23D0           58 System.String
         26F240C           60 System.String
         26F2448           50 System.String
         26F247C           12 ImageApplication.CustomImage
         26F2488           86 System.String
         26F24E0           84 System.Char[]
         26F2534           28 System.Char[]
         26F2550           84 System.Char[]
         26F25A4           88 System.Char[]
         26F25FC           94 System.Char[]
         26F265C           76 System.Char[]
         26F26A8           24 System.String
         26F26C0           24 System.String
         26F26D8           42 System.String
         26F27C0           16 System.Object[]
         26F27D0           14 System.Char[]
         26F27E0           14 System.Char[]
         26F27F0           16 System.String
         26F2888           12 System.Security.Permissions.SecurityPermission
         26F2894           36 System.Security.Util.TokenBasedSet
         26F28B8           32 System.Security.PermissionTokenFactory
         26F28D8           84 System.Security.PermissionToken[]
         26F292C           52 System.Collections.Hashtable
         26F2960           96 System.Collections.Hashtable+bucket[]
         26F29C0           52 System.Collections.Hashtable
         26F29F4           96 System.Collections.Hashtable+bucket[]
         26F2A54           20 System.Security.PermissionToken
         26F2A88           28 System.RuntimeType
         26F2ACC           28 System.RuntimeType
         26F2B00           24 System.OperatingSystem
         26F2B18           24 System.Version
         26F2B30           24 System.Version
         26F2C8C           56 System.String
         26F2CC4           92 System.String
         26F2D20           44 System.Diagnostics.TraceSwitch
         26F2D4C           16 System.String
         26F2D5C           24 System.Collections.Generic.List<System.WeakReference>
         26F2D74           16 System.WeakReference[]
         26F2D84           12 System.WeakReference
         26F2D90           32 System.WeakReference[]
         26F2DB0           32 System.EventHandler
         26F2DD0           24 System.Drawing.Bitmap
         26F2E00           90 System.String
         26F2E5C           90 System.String
         26F2EB8           90 System.String
         26F2F14           90 System.String
         26F2F70           90 System.String
         26F2FCC           90 System.String
         26F3028           90 System.String
         26F3084           90 System.String
         26F30E0           90 System.String
         26F313C           90 System.String
         26F3198           90 System.String
         26F31F4           90 System.String
         26F3250           24 System.Guid
         26F3268           60 System.String
         26F32A4           48 System.String
         26F32D4           48 System.String
         26F3304           58 System.String
         26F3340           24 System.Drawing.Imaging.ImageFormat
         26F3358           24 System.Drawing.Imaging.ImageFormat
         26F3370           24 System.Drawing.Imaging.ImageFormat
         26F3388           24 System.Drawing.Imaging.ImageFormat
         26F33A0           24 System.Drawing.Imaging.ImageFormat
         26F33B8           24 System.Drawing.Imaging.ImageFormat
         26F33D0           24 System.Drawing.Imaging.ImageFormat
         26F33E8           24 System.Drawing.Imaging.ImageFormat
         26F3400           24 System.Drawing.Imaging.ImageFormat
         26F3418           24 System.Drawing.Imaging.ImageFormat
         26F3430           24 System.Drawing.Imaging.ImageFormat
         26F3448           24 System.Drawing.Imaging.ImageFormat
         26F3460           28 Microsoft.Win32.Win32Native+InputRecord
         26F347C           32 System.Text.UnicodeEncoding
         26F349C           16 System.String
         26F34AC           16 System.Text.EncoderReplacementFallback
         26F34BC           16 System.Text.DecoderReplacementFallback
         26F34CC           12 System.Object
         26F34D8           20 Microsoft.Win32.SafeHandles.SafeFileHandle
         26F34EC           12 System.Object
         26F34F8           12 System.Object
         26F3504           52 System.Collections.Hashtable
         26F3538           48 System.Collections.Hashtable+bucket[]
         26F3574           40 System.String
         26F359C           28 System.RuntimeType
         26F35B8           28 System.Reflection.RuntimeAssembly
         26F361C           30 System.String
         26F363C           26 System.String
         26F3658           72 System.Text.SBCSCodePageEncoding
         26F36A0           20 System.Text.InternalEncoderBestFitFallback
         26F36B4           24 System.Text.InternalDecoderBestFitFallback
         26F36CC           78 System.String
         26F371C           72 System.Globalization.CultureInfo
         26F3764           16 System.String
         26F3774           16 System.String
         26F3784           16 System.String
         26F3794           16 System.String
         26F37A4           90 System.String
         26F3800           18 System.String
         26F3814           50 System.String
         26F3848           18 System.String
         26F385C           48 System.String
         26F388C           16 System.String
         26F389C           16 System.String
         26F38AC           16 System.String
         26F38BC           16 System.String
         26F38CC           16 System.String
         26F38DC           16 System.String
         26F38EC           16 System.String
         26F38FC           16 System.String
         26F390C           16 System.String
         26F391C           16 System.String
         26F392C           20 System.String
         26F3940           30 System.String
         26F3960           32 System.String
         26F3980           16 System.String
         26F3990           16 System.String
         26F39A0           20 System.String
         26F39B4           68 System.String
         26F39F8           18 System.String
         26F3A0C           18 System.String
         26F3A20           30 System.String
         26F3A40           24 System.String
         26F3A58           30 System.String
         26F3A78           22 System.String
         26F3A90           28 System.String
         26F3AAC           24 System.String
         26F3AC4           20 System.String
         26F3AD8           20 System.String
         26F3AEC           20 System.String
         26F3B00           20 System.String
         26F3B14           40 System.String
         26F3B3C          114 System.String
         26F3BB0           24 System.Version
         26F3BC8          308 System.Globalization.CultureData
         26F3CFC           56 System.String[]
         26F3D34           16 System.Int32[]
         26F3D44           16 System.Int32[]
         26F3D54           20 System.String[]
         26F3D68           32 System.String[]
         26F3D88           20 System.String[]
         26F3D9C           16 System.Int32[]
         26F3DAC          108 System.Globalization.CalendarData[]
         26F3E18           50 System.String
         26F3E4C           34 System.String
         26F3E70           34 System.String
         26F3E94           50 System.String
         26F3EC8           32 System.String
         26F3EE8           28 System.String
         26F3F04           22 System.String
         26F3F1C           18 System.String
         26F3F30           26 System.String
         26F3F4C           26 System.String
         26F3F68           28 System.String
         26F3F84           32 System.String
         26F3FA4           30 System.String
         26F3FC4           26 System.String
         26F3FE0           30 System.String
         26F4000           20 System.String
         26F4014           20 System.String
         26F4028           20 System.String
         26F403C           20 System.String
         26F4050           20 System.String
         26F4064           20 System.String
         26F4078           20 System.String
         26F408C           18 System.String
         26F40A0           18 System.String
         26F40B4           18 System.String
         26F40C8           18 System.String
         26F40DC           18 System.String
         26F40F0           18 System.String
         26F4104           18 System.String
         26F4118           28 System.String
         26F4134           30 System.String
         26F4154           24 System.String
         26F416C           24 System.String
         26F4184           20 System.String
         26F4198           22 System.String
         26F41B0           22 System.String
         26F41C8           26 System.String
         26F41E4           32 System.String
         26F4204           28 System.String
         26F4220           30 System.String
         26F4240           30 System.String
         26F4260           20 System.String
         26F4274           20 System.String
         26F4288           20 System.String
         26F429C           20 System.String
         26F42B0           20 System.String
         26F42C4           20 System.String
         26F42D8           20 System.String
         26F42EC           20 System.String
         26F4300           20 System.String
         26F4314           20 System.String
         26F4328           20 System.String
         26F433C           84 System.Globalization.CalendarData
         26F4390           24 System.String[]
         26F43A8           20 System.String[]
         26F43BC           20 System.String[]
         26F43D0           20 System.String[]
         26F43E4           20 System.String[]
         26F43F8           20 System.String[]
         26F440C           44 System.String[]
         26F4438           44 System.String[]
         26F4464           44 System.String[]
         26F4490           68 System.String[]
         26F44D4           68 System.String[]
         26F4518           26 System.String
         26F4534           26 System.String
         26F4568           72 System.Globalization.CultureInfo
         26F45E4           24 System.String
         26F45FC           48 System.Collections.Generic.Dictionary<System.String,System.Globalization.CultureData>
         26F462C           28 System.RuntimeType
         26F4648           28 System.RuntimeType
         26F4664           12 System.Collections.Generic.GenericEqualityComparer<System.String>
         26F4670          308 System.Globalization.CultureData
         26F47A4           24 System.String
         26F47BC           24 System.String
         26F47D4           24 System.String
         26F47EC           24 System.String
         26F4804           24 System.String
         26F4834           12 System.Object
         26F4840           24 System.Int32[]
         26F4858           60 System.Collections.Generic.Dictionary+Entry<System.String,System.Globalization.CultureData>[]
         26F48AC           72 System.Globalization.CultureInfo
         26F4928           24 System.String
         26F4940          308 System.Globalization.CultureData
         26F4A74           24 System.String
         26F4A8C           24 System.String
         26F4AA4           24 System.String
         26F4ABC           24 System.String
         26F4B34           22 System.String
         26F4B4C           26 System.String
         26F4B68           52 System.Object[]
         26F4B9C           28 System.Text.StringBuilder
         26F4BB8          156 System.Char[]
         26F4C54           28 System.RuntimeType
         26F4C70           28 System.RuntimeType
         26F4C8C           28 System.RuntimeType
         26F4CA8          132 System.Globalization.NumberFormatInfo
         26F4D94           16 System.String
         26F4E38           20 Microsoft.Win32.SafeHandles.SafeViewOfFileHandle
         26F4E4C           20 Microsoft.Win32.SafeHandles.SafeFileMappingHandle
         26F4E60           12 System.Int32
         26F4E6C           28 System.IO.__ConsoleStream
         26F4E88           20 System.IO.Stream+NullStream
         26F4E9C           60 System.IO.StreamWriter
         26F4ED8           30 System.String
         26F4EF8           26 System.String
         26F4F14           16 System.Char[]
         26F4F24           28 System.Text.EncoderNLS
         26F4F40          524 System.Char[]
         26F514C          269 System.Byte[]
         26F525C           24 System.String
         26F5274           24 System.String
         26F528C           32 System.String
         26F52AC           32 System.String
         26F52CC           32 System.String
         26F52EC           24 System.IO.TextWriter+SyncTextWriter
         26F5304           52 System.Threading.Thread
         26F5338           16 System.Char[]
         26F5384           24 System.String
         26F539C           26 System.String
         26F53B8           20 Microsoft.Win32.SafeHandles.SafeFileHandle
         26F53D8           28 System.IO.__ConsoleStream
         26F53F4           64 System.IO.StreamReader
         26F5434           28 System.Text.DecoderNLS
         26F5450          268 System.Byte[]
         26F555C          524 System.Char[]
         26F5768           12 System.Byte[]
         26F5774           16 System.IO.TextReader+SyncTextReader
         36F1020        4,456 System.Object[]
         36F2198          528 System.Object[]
         36F23B8        4,096 System.Object[]
         36F33C8        8,176 System.Object[]
Stats
        Size        Count Type
          12            1 System.Collections.Generic.ObjectEqualityComparer<System.Type>
          12            1 ImageApplication.CustomImage
          12            1 System.Security.Permissions.SecurityPermission
          12            1 System.WeakReference
          12            1 System.Collections.Generic.GenericEqualityComparer<System.String>
          12            1 System.Int32
          16            1 System.Security.Policy.AssemblyEvidenceFactory
          16            1 System.Text.EncoderReplacementFallback
          16            1 System.Text.DecoderReplacementFallback
          16            1 System.IO.TextReader+SyncTextReader
          20            1 Microsoft.Win32.SafeHandles.SafePEFileHandle
          20            1 System.Security.PermissionToken
          20            1 System.Text.InternalEncoderBestFitFallback
          20            1 Microsoft.Win32.SafeHandles.SafeViewOfFileHandle
          20            1 Microsoft.Win32.SafeHandles.SafeFileMappingHandle
          20            1 System.IO.Stream+NullStream
          24            1 System.OperatingSystem
          24            1 System.Collections.Generic.List<System.WeakReference>
          24            1 System.Drawing.Bitmap
          24            1 System.Guid
          24            1 System.Text.InternalDecoderBestFitFallback
          24            1 System.IO.TextWriter+SyncTextWriter
          28            1 System.SharedStatics
          28            1 Microsoft.Win32.Win32Native+InputRecord
          28            1 System.Text.StringBuilder
          28            1 System.Text.EncoderNLS
          28            1 System.Text.DecoderNLS
          32            1 System.Security.Policy.PEFileEvidenceFactory
          32            1 System.Security.PermissionTokenFactory
          32            1 System.EventHandler
          32            1 System.Text.UnicodeEncoding
          36            1 System.Security.PermissionSet
          36            1 System.Security.Util.TokenBasedSet
          40            1 System.Security.Policy.Evidence
          40            2 Microsoft.Win32.SafeHandles.SafeFileHandle
          44            1 System.Threading.ReaderWriterLock
          44            1 System.Diagnostics.TraceSwitch
          48            1 System.Collections.Generic.Dictionary<System.Type,System.Security.Policy.EvidenceTypeDescriptor>
          48            2 System.WeakReference[]
          48            1 System.Collections.Generic.Dictionary<System.String,System.Globalization.CultureData>
          52            1 System.Threading.Thread
          56            1 System.Type[]
          56            2 System.Reflection.RuntimeAssembly
          56            2 System.IO.__ConsoleStream
          60            1 System.Collections.Generic.Dictionary+Entry<System.String,System.Globalization.CultureData>[]
          60            1 System.IO.StreamWriter
          64            1 System.IO.StreamReader
          68            1 System.AppDomainSetup
          72            6 System.Object
          72            3 System.Version
          72            1 System.Text.SBCSCodePageEncoding
          84            1 System.Exception
          84            1 System.OutOfMemoryException
          84            1 System.StackOverflowException
          84            1 System.ExecutionEngineException
          84            1 System.Security.PermissionToken[]
          84            1 System.Globalization.CalendarData
         108            1 System.Globalization.CalendarData[]
         112            1 System.AppDomain
         132            1 System.Globalization.NumberFormatInfo
         156            3 System.Collections.Hashtable
         168            2 System.Threading.ThreadAbortException
         216            3 System.Globalization.CultureInfo
         240            3 System.Collections.Hashtable+bucket[]
         284            1 System.Collections.Generic.Dictionary+Entry<System.Type,System.Security.Policy.EvidenceTypeDescriptor>[]
         288           12 System.Drawing.Imaging.ImageFormat
         452            6 System.Int32[]
         549            3 System.Byte[]
         624           17 System.String[]
         784           28 System.RuntimeType
         924            3 System.Globalization.CultureData
       1,718           13 System.Char[]
       6,628          180 System.String
      17,324            6 System.Object[]

Total: 32,951 bytes in 351 objects

 

Following blog from where I started reading about CLRMD

http://gigi.nullneuron.net/gigilabs/analyzing-live-process-memory-with-clrmd/#comment-8857

Many thanks to gigi labs for posting and updating the code.

For More info and good part of example code in this Blog  is taken from CLRMD examples, please visit:

https://github.com/Microsoft/dotnetsamples/tree/master/Microsoft.Diagnostics.Runtime/CLRMD

 

Written by Harsha

December 29, 2015 at 12:55 PM

Posted in C Sharp

Yet another Blog on BDD

leave a comment »

Here I am documenting BDD and This experiment is more like setting up BDD using SpecFlow in Visual Studio 2013. One should be able to set up BDD in 15Mins.

Important links on BDD:

  1. From Wiki
  2. Dan North

So lets Set up the SpecFlow(http://www.specflow.org/getting-started/)

First search for SpecFlow in VS2013

1

Search for SpecFlow

2. Add a Unit testing Project and Add Feature File to project

Add Spec Runner to test project

Add Feature file to unit test project

3. Spec runner is require to run the BDD. By Default, Speeflow is configured to run using nUnit.

Add Spec Runner to test project

Add Spec Runner to test project

4. Probably you don’t need this step.

Another way to add is from Nuget manager’s console

Another way to add is from Nuget manager’s console

5. Now we are all Set. I have a small application called Sign up, where user enters first name, second name, Date of Birth and also email id. If user is more than 18 years and chosen email id is not found in our DB, user is allowed to sign up. Of-course, first and second name should not be empty. In-fact, this is our condition and business requirement.

SignupApp

Sign up Application

6. Open the Spec flow Feature file and add your business requirement:

Business requirement

First Business requirement

7.  Now run all test… Because we don’t have any test written, spec flow give you a suggestion:

SuggestionFromSpecFlow

Suggestion From SpecFlow

8. Copy this code to a new class file in the same project and add your Test cases and Run it.  In this project it is more like setting the Spec flow for BDD. I had a application for which I want to write the Test cases. In BDD, We write the Business requirement and then you write the logic and this logic may be in more than one class or more than one module. At the same time you have TDD, Where you will writing small test which covers piece of code for various scenarios.

Best part of BDD is the report, your business or product owner can always see the progress of the work.

Report

Spec Flow Report

Thanks for going thru the blog.

Link to complete project:Signup on OneDrive

 

 

Written by Harsha

December 8, 2015 at 9:58 AM

Posted in C Sharp

Multi – Core Jitting

leave a comment »

As we all know, application start up is crucial and minimizing lunching time  is a challenging and interesting. Usually, startup is  processed behind splash screen and update the status through messages. But as many know, machine code is not ready and it will be available just before it is executed. So machine code is generated through JIT Compilation but it is the same line of code executed every time on same machine and spending same amount of time. The JIT Code generated its all lost when you close the application.  Before we jump to .net feature, brief intro to JIT Compilation.

What is JIT Compilation?
As you all know, on .net, Code is compiled into IL code and it is not Machine code. In order to execute the a piece of code, that should be available in machine code. JIT made it available during run time i.e, converts IL Code to Machine code during run time. That means, you have to wait for your code to execute. Waiting time may be very very small, but still one need to wait.

Whats the Solution?
Multi-core Jitting in .net 4.5

string path = Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)).FullName;
ProfileOptimization.SetProfileRoot(path); // C:/Users/user/AppData
ProfileOptimization.StartProfile("Startup.Profile"); // this file is created.

When application is launched for first JIT produces Machine code and startup.profile file written when application exists. On consecutive application launches uses this profile file and avoids JITTing.

For more info please refer:

https://blogs.msdn.microsoft.com/dotnet/2012/10/18/an-easy-solution-for-improving-app-launch-performance/

 

 

Written by Harsha

December 21, 2014 at 11:28 PM

Posted in C Sharp

Getting Property/Method Name

leave a comment »

Hope you are using INotifyPropertyChanged and also implemented same. When we are raising the event, we also need to supply the property name, usually we hard-code it. So this article about getting the property name from within the property block(setter block).

Here is the code and comments:

    private string _BindedStringProp;

        public string BindedStringProp
        {
            get 
            { 
                return _BindedStringProp; 
            }
            set 
            { 
                _BindedStringProp = value;
                //Following line, RawpropName will have value "set_BindedStringProp"
                string RawpropName = MethodBase.GetCurrentMethod().Name;
                //but we need correct property name which will get when we remove first 4 characters.
                string RequiredPropName = MethodBase.GetCurrentMethod().Name.Substring(4); 
                //Following is best way to get the Property Method.
                RaisePropertyChanged();
            }
        }

        private void RaisePropertyChanged([CallerMemberName] string propertyName = "")
        {
            //[CallerMemberName] is doing all the magic.
            if (PropertyChanged != null && !string.IsNullOrEmpty(propertyName))
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }

            //But when you check the same in a method, we will get correct method name and 
            //no substring is required.
            string propName = MethodBase.GetCurrentMethod().Name;
            
        }

Please leave your comments.

Thank you.

Written by Harsha

August 27, 2014 at 12:46 AM

Posted in C Sharp, WPF