Created
October 7, 2015 17:16
-
-
Save chamons/34d419820a1ae1248a6d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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