diff --git a/Source/ACE.Server/Command/Handlers/DeveloperCommands.cs b/Source/ACE.Server/Command/Handlers/DeveloperCommands.cs index 2f7c1810ef..cad9e528d1 100644 --- a/Source/ACE.Server/Command/Handlers/DeveloperCommands.cs +++ b/Source/ACE.Server/Command/Handlers/DeveloperCommands.cs @@ -2,8 +2,9 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; -using System.Threading.Tasks; using System.Numerics; +using System.Runtime; +using System.Threading.Tasks; using log4net; @@ -2213,8 +2214,18 @@ public static void HandleClearPhysicsCaches(Session session, params string[] par public static void HandleForceGC(Session session, params string[] parameters) { GC.Collect(); + } + + [CommandHandler("forcegc2", AccessLevel.Developer, CommandHandlerFlag.None, 0, "Forces .NET Garbage Collection with LOH Compact")] + public static void HandleForceGC2(Session session, params string[] parameters) + { + // https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/fundamentals + // https://learn.microsoft.com/en-us/dotnet/api/system.runtime.gcsettings.largeobjectheapcompactionmode + GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce; + + GC.Collect(); - CommandHandlerHelper.WriteOutputInfo(session, ".NET Garbage Collection forced"); + CommandHandlerHelper.WriteOutputInfo(session, ".NET Garbage Collection forced with LOH Compact"); } [CommandHandler("auditobjectmaint", AccessLevel.Developer, CommandHandlerFlag.None, 0, "Iterates over physics objects to find leaks")] diff --git a/Source/ACE.Server/Program.cs b/Source/ACE.Server/Program.cs index c45dfd2eea..4224e0aa48 100644 --- a/Source/ACE.Server/Program.cs +++ b/Source/ACE.Server/Program.cs @@ -2,6 +2,7 @@ using System.Globalization; using System.IO; using System.Reflection; +using System.Runtime; using System.Runtime.InteropServices; using System.Threading; @@ -314,8 +315,14 @@ public static void Main(string[] args) // Free up memory before the server goes online. This can free up 6 GB+ on larger servers. log.Info("Forcing .net garbage collection..."); - for (int i = 0 ; i < 10 ; i++) + for (int i = 0; i < 10; i++) + { + // https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/fundamentals + // https://learn.microsoft.com/en-us/dotnet/api/system.runtime.gcsettings.largeobjectheapcompactionmode + GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce; + GC.Collect(); + } // This should be last log.Info("Initializing CommandManager...");