Skip to content

Instantly share code, notes, and snippets.

@chamons
Created October 7, 2015 17:16
Show Gist options
  • Save chamons/34d419820a1ae1248a6d to your computer and use it in GitHub Desktop.
Save chamons/34d419820a1ae1248a6d to your computer and use it in GitHub Desktop.
// A quick and dirty "how often is the GC being called, in 1/4 second increments
// Useful if you think your allocation pattern is hurting but can't load up in a real profiler
static int currentGC = 0;
static int currentCount = 0;
static void Main (string[] args)
{
Task.Factory.StartNew (() => {
while (true)
{
int now = GC.CollectionCount (0);
if (currentGC == now) {
currentCount++;
}
else
{
Console.WriteLine (/*"Gen {0} - */"{1}", currentGC, currentCount);
currentGC = now;
currentCount = 1;
}
Thread.Sleep (250);
}
});
}
// A "fake NSView" of different colors you can replace views with to
// rule out slow views being the problem.
NSView MakeFakeView ()
{
NSView v = new NSView (new CoreGraphics.CGRect (0, 0, 0, 0));
v.WantsLayer = true;
v.Layer.BackgroundColor = GenerateRandomColor ();
return v;
}
CGColor GenerateRandomColor()
{
Random random = new Random();
double red = random.NextDouble ();
double green = random.NextDouble ();
double blue = random.NextDouble ();
red = (red + 1) / 2;
green = (green + 1) / 2;
blue = (blue + 1) / 2;
return new CGColor((nfloat)red, (nfloat)green, (nfloat)blue);
}
// Sometimes Console.WriteLine doesn't work, due to redirection, caches not flushing, or other such nonsense
// You can use this to dump to a file
public static class DebugCWL
{
[System.Runtime.InteropServices.DllImport ("__Internal")]
static extern System.IntPtr NSHomeDirectory ();
static string HomeDirectory
{
get
{
NSString s = ObjCRuntime.Runtime.GetNSObject<NSString> (NSHomeDirectory ());
return (string)s;
}
}
public static void WriteLine (string s)
{
System.IO.File.AppendAllText (System.IO.Path.Combine (HomeDirectory, "DebugCWL.txt"), s + "\n");
}
public static void WriteLine (string s, params object [] args)
{
DebugCWL.WriteLine (string.Format (s, args));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment