Skip to content

Instantly share code, notes, and snippets.

@mikeminutillo
Created November 27, 2013 07:05
Show Gist options
  • Save mikeminutillo/7671745 to your computer and use it in GitHub Desktop.
Save mikeminutillo/7671745 to your computer and use it in GitHub Desktop.
Regions to assist with debugging applications
public class MemoryProfileRegion : IDisposable
{
private static readonly ILog Log = LogManager.GetLogger("Profiling");
private readonly string _name;
private readonly long _initialMemory;
private MemoryProfileRegion(string name)
{
_name = name;
if(!Log.IsDebugEnabled) return;
Thread.MemoryBarrier();
_initialMemory = System.GC.GetTotalMemory(true);
}
public void Dispose()
{
if(!Log.IsDebugEnabled) return;
Thread.MemoryBarrier();
var finalMemory = System.GC.GetTotalMemory(true);
var consumed = finalMemory - _initialMemory;
Log.DebugFormat("{0} consumed {1:###,###,###,##0} bytes of memory", _name, consumed);
}
public static MemoryProfileRegion Named(string name)
{
return new MemoryProfileRegion(name);
}
}
public class ProfileRegion : IDisposable
{
private static readonly ILog Log = LogManager.GetLogger("Profiling");
private readonly string _name;
private readonly Stopwatch _stopwatch = new Stopwatch();
private ProfileRegion(string name)
{
_name = name;
_stopwatch.Start();
Log.DebugFormat("Entered {0}", _name);
}
public void Dispose()
{
_stopwatch.Stop();
Log.DebugFormat("Exited {0}. Took {1} ms", _name, _stopwatch.ElapsedMilliseconds);
}
public static ProfileRegion Named(string name)
{
return new ProfileRegion(name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment