static void Main(string[] args) { NErrorLog.Enabled = false; NSucceedLog.Enabled = false; NWarningLog.Enabled = false; Stopwatch watch = new Stopwatch(); double tempTotleTime; #region Natasha Preheating var preTime = Process.GetCurrentProcess().TotalProcessorTime; watch.Start(); tempType = NClass.Create("tes1t") .Namespace("Test") .UseRandomName() .PublicField <string>("Name") .PublicField <string>("Age") .PublicField <int[]>("Temp") .Ctor(item => item.Body("Temp = new int[40960];")) .GetType(); watch.Stop(); tempTotleTime = GetCpu(Process.GetCurrentProcess().TotalProcessorTime, preTime); if (watch.Elapsed.Seconds > 0) { tempTotleTime = tempTotleTime / watch.Elapsed.Seconds; } Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine("Natasha预热:"); Console.WriteLine("-----------------------------------------------------------------------------------------"); Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|"); Console.WriteLine("-----------------------------------------------------------------------------------------"); #endregion Thread.Sleep(1000); #region Run Compiler preTime = Process.GetCurrentProcess().TotalProcessorTime; watch.Restart(); Test(); watch.Stop(); tempTotleTime = GetCpu(Process.GetCurrentProcess().TotalProcessorTime, preTime); if (watch.Elapsed.Seconds > 0) { tempTotleTime = tempTotleTime / watch.Elapsed.Seconds; } Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"{count}个独立域编译后:"); Console.WriteLine("-----------------------------------------------------------------------------------------"); Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|"); Console.WriteLine("-----------------------------------------------------------------------------------------"); #endregion Thread.Sleep(1000); #region Release Handler preTime = Process.GetCurrentProcess().TotalProcessorTime; watch.Restart(); Release(); watch.Stop(); tempTotleTime = GetCpu(Process.GetCurrentProcess().TotalProcessorTime, preTime); if (watch.Elapsed.Seconds > 0) { tempTotleTime = tempTotleTime / watch.Elapsed.Seconds; } Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("释放中:"); Console.WriteLine("-----------------------------------------------------------------------------------------"); Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|"); Console.WriteLine("-----------------------------------------------------------------------------------------"); #endregion Thread.Sleep(1000); #region Run GC preTime = Process.GetCurrentProcess().TotalProcessorTime; watch.Restart(); RunGc(); watch.Stop(); tempTotleTime = GetCpu(Process.GetCurrentProcess().TotalProcessorTime, preTime); if (watch.Elapsed.Seconds > 0) { tempTotleTime = tempTotleTime / watch.Elapsed.Seconds; } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(); Console.WriteLine("回收后:"); Console.WriteLine("-----------------------------------------------------------------------------------------"); Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|"); Console.WriteLine("-----------------------------------------------------------------------------------------"); #endregion Thread.Sleep(1000); #region Check Alive preTime = Process.GetCurrentProcess().TotalProcessorTime; watch.Restart(); var alive = CheckAlive(); DomainManagement.Clear(); watch.Stop(); tempTotleTime = GetCpu(Process.GetCurrentProcess().TotalProcessorTime, preTime); if (watch.Elapsed.Seconds > 0) { tempTotleTime = tempTotleTime / watch.Elapsed.Seconds; } Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine(); Console.WriteLine($"存活检测: {alive}"); Console.WriteLine("-----------------------------------------------------------------------------------------"); Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|"); Console.WriteLine("-----------------------------------------------------------------------------------------"); #endregion //for (int i = 0; i < 10; i++) //{ // Thread.Sleep(3000); // preTime = Process.GetCurrentProcess().TotalProcessorTime; // Console.WriteLine($"第{i}次静默检测:"); // Console.WriteLine("-----------------------------------------------------------------------------------------"); // Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|"); // Console.WriteLine("-----------------------------------------------------------------------------------------"); //} Console.ReadKey(); }