Skip to content

Commit

Permalink
Removed WeakReference<IHosts> already cleaned up by GC (#4995)
Browse files Browse the repository at this point in the history
* Removing unused IHosts

* Removed unused hosts in both Register methods
  • Loading branch information
mstfbl authored Apr 4, 2020
1 parent 77e7f98 commit 820f1e2
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Microsoft.ML.Core/Environment/HostEnvironmentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,18 @@ public HostBase(HostEnvironmentBase<TEnv> source, string shortName, string paren
Depth = source.Depth + 1;
}

/// <summary>
/// This method registers and returns the host for the calling component. The generated host is also
/// added to <see cref="_children"/> and encapsulated by <see cref="WeakReference"/>. It becomes
/// necessary to remove these hosts when they are reclaimed by the Garbage Collector.
/// </summary>
public new IHost Register(string name, int? seed = null, bool? verbose = null)
{
Contracts.CheckNonEmpty(name, nameof(name));
IHost host;
lock (_cancelLock)
{
_children.RemoveAll(r => r.TryGetTarget(out IHost _) == false);
Random rand = (seed.HasValue) ? RandomUtils.Create(seed.Value) : RandomUtils.Create(_rand);
host = RegisterCore(this, name, Master?.FullName, rand, verbose ?? Verbose);
if (!IsCanceled)
Expand Down Expand Up @@ -385,12 +391,18 @@ protected HostEnvironmentBase(HostEnvironmentBase<TEnv> source, Random rand, boo
_children = new List<WeakReference<IHost>>();
}

/// <summary>
/// This method registers and returns the host for the calling component. The generated host is also
/// added to <see cref="_children"/> and encapsulated by <see cref="WeakReference"/>. It becomes
/// necessary to remove these hosts when they are reclaimed by the Garbage Collector.
/// </summary>
public IHost Register(string name, int? seed = null, bool? verbose = null)
{
Contracts.CheckNonEmpty(name, nameof(name));
IHost host;
lock (_cancelLock)
{
_children.RemoveAll(r => r.TryGetTarget(out IHost _) == false);
Random rand = (seed.HasValue) ? RandomUtils.Create(seed.Value) : RandomUtils.Create(_rand);
host = RegisterCore(this, name, Master?.FullName, rand, verbose ?? Verbose);
_children.Add(new WeakReference<IHost>(host));
Expand Down

0 comments on commit 820f1e2

Please sign in to comment.