// There's probably a better way to do this but using top is easy and should give us // accurate results. Note that Process does have a number of memory related properties // but they all return 0 with mono 1.9 except Process.MaxWorkingSet. private void DoPrintMemory() { GC.Collect(); GC.WaitForPendingFinalizers(); // Get our pid. string pid = Process.GetCurrentProcess().Id.ToString(); // Get the result of running top. ProcessStartInfo info = new ProcessStartInfo(); info.Arguments = "-l 1 -stats pid,command,rsize,vsize -pid " + pid; // top has changed a lot in Snow Leopard... // info.Arguments = "-l 1 -P 'PID COMMAND RSIZE VSIZE' -p '^aaaa ^bbbbbbbbbbbb $jjjjjjjjjj $llllllllll'";; info.FileName = "top"; info.RedirectStandardOutput = true; info.UseShellExecute = false; Process process = Process.Start(info); if (!process.WaitForExit(60 * 1000)) { throw new ApplicationException("timed out waiting for top."); } if (process.ExitCode < 0 || process.ExitCode > 1) { throw new ApplicationException("top failed with error " + process.ExitCode + "."); } // Find the line in the top output that starts with our pid. The line will be // formatted like this: // 4327 ntpd 708K+ 2378M+ while (!process.StandardOutput.EndOfStream) { string line = process.StandardOutput.ReadLine(); if (line.StartsWith(pid)) { string[] parts = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length == 4) { Console.WriteLine("timestamp: {0:hh:mm}", DateTime.Now); Console.WriteLine("resident bytes: {0:0.000}M", DoParseBytes(parts[2])); Console.WriteLine("virtual bytes: {0:0.000}M", DoParseBytes(parts[3])); Console.WriteLine("managed bytes: {0:0.000}M", GC.GetTotalMemory(false) / OneMeg); Console.WriteLine("NSObject count: {0}", NSObject.Snapshot().Length); Console.WriteLine(" "); } else { Console.WriteLine("Expected four columns from top, but got '{0}'", line); } return; } } Console.WriteLine("Couldn't find our pid: {0}.", pid); }
public void dumpObjects(NSObject sender) { List <string> lines = new List <string>(); GC.Collect(); GC.WaitForPendingFinalizers(); foreach (NSObject o in NSObject.Snapshot()) { lines.Add(o.ToString("G", null)); } lines.Sort(); foreach (string line in lines) { Console.WriteLine(line); } Console.WriteLine(" "); }