Skip to content

Commit

Permalink
Enable LOH Compacting on forcegc (#4101)
Browse files Browse the repository at this point in the history
* Enable LOH Compacting on forcegc

Maybe this will help recover long running servers that go into GC sawtooth pattern

* This should be good for master
  • Loading branch information
Mag-nus authored Feb 3, 2024
1 parent 63dcd84 commit 8c5d81a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
15 changes: 13 additions & 2 deletions Source/ACE.Server/Command/Handlers/DeveloperCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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")]
Expand Down
9 changes: 8 additions & 1 deletion Source/ACE.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Threading;

Expand Down Expand Up @@ -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...");
Expand Down

0 comments on commit 8c5d81a

Please sign in to comment.