Skip to content

Memory keeps growing when using InProcessTestCluster — stopped silos never seem to get garbage collected #10258

Closed

Description

@JoshHermanAngstrom

I have a fairly large component test suite where each test class spins up its own InProcessTestCluster (single silo) and tears it down afterwards. I noticed that memory usage just keeps climbing for the whole test run — it got up to about 17GB peak for a run of ~300 tests, and only comes back down when the test process exits. I also noticed the thread count keeps growing during the run, and a lot of the threads are named Orleans.Runtime.Watchdog.Platform and Orleans.Runtime.Watchdog.Component — it looks like I end up with 2 more of these for every test class that runs, and they never go away.

My teardown looks like this:

await cluster.StopAllSilosAsync();
await cluster.DisposeAsync();

To my knowledge this is the correct way to shut down a cluster, and I assumed that disposing the cluster first was the correct thing to do, but maybe that is supposed to be left to DisposeAsync?

What I've managed to dig up

I wrote a small test to check whether the silo actually gets collected after teardown, and it doesn't — the WeakReference stays alive no matter how hard I GC:

[Fact]
public async Task StoppedSilo_AfterClusterDispose_ShouldBeCollectible()
{
    var refs = new List<WeakReference>();
    for (var i = 0; i < 5; i++)
    {
        var builder = new InProcessTestClusterBuilder(1);
        var cluster = builder.Build();
        await cluster.DeployAsync();
        refs.Add(new WeakReference(cluster.Silos[0].ServiceProvider));
        await cluster.StopAllSilosAsync();
        await cluster.DisposeAsync();
        for (var j = 0; j < 3; j++)
        {
            GC.Collect(2, GCCollectionMode.Forced, blocking: true, compacting: true);
            GC.WaitForPendingFinalizers();
        }
        // prints 1/1, 2/2, 3/3, 4/4, 5/5 — every silo is still reachable
        Console.WriteLine($"alive: {refs.Count(r => r.IsAlive)}/{refs.Count}");
    }
}

I ran some analysis on a few gcdumps and the retained silos all seem to hang off the watchdog threads. The root path from the profiler looks like this (thread stack → watchdog → eventually the whole ServiceProvider):

[Thread stack] Orleans.Runtime.Watchdog
  → List<Orleans.Runtime.IHealthCheckParticipant>
  → Orleans.Runtime.MembershipService.MembershipTableManager
  → Orleans.Runtime.SiloLifecycleSubject
  → ... → Orleans.Runtime.Silo
  → ServiceProviderEngineScope
  → Microsoft.Extensions.DependencyInjection.ServiceProvider

I was reading through the TestingHost source to try and get an understanding of it, and I think I might see why this is happening but honestly I'm not sure if I'm misreading it.

In InProcessSiloHandle, StopSiloAsync sets isActive = false in its finally block, and then DisposeAsync starts with if (!this.IsActive) return; — so if the silo was already stopped (like mine are, because I call StopAllSilosAsync first), it looks like SiloHost never gets disposed at all? And if the host is never disposed, presumably the ServiceProvider never disposes the Watchdog, which would explain why its threads keep running. But I might be missing somewhere else that the host is supposed to be cleaned up.

What makes me think it's the host disposal: if I grab the SiloHost off each handle and dispose it manually after the cluster teardown, the leak goes away completely — the WeakReferences die, the watchdog threads exit, and my test suite peak went from ~17GB down to ~4.5GB (and runs about 3x faster, presumably from not dragging a huge heap around).

// workaround I'm currently using
var siloHosts = cluster.Silos.OfType<InProcessSiloHandle>().Select(h => h.SiloHost).ToList();
await cluster.StopAllSilosAsync();
await cluster.DisposeAsync();
foreach (var host in siloHosts)
{
    if (host is IAsyncDisposable ad) await ad.DisposeAsync();
    else host.Dispose();
}

Environment

  • Microsoft.Orleans.TestingHost 10.1.0 and 10.2.1 (reproduced on both; on 10.1.0 the retained memory per silo was quite a bit bigger)
  • .NET 10, Windows

Is calling StopAllSilosAsync before DisposeAsync just not a supported pattern? If it is supposed to work, should DisposeAsync be disposing the SiloHost even when the silo was already stopped?

Activity

  1. ReubenBond commented on Jul 16, 2026

    @ReubenBond
    Member

    Fixed in #10272. Thank you for reporting and investigating, @JoshHermanAngstrom!

  2. locked and limited conversation to collaborators on Aug 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions