-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #456 from zjklee/issues/452
Fix memory leak at Configuration subscribers
- Loading branch information
Showing
8 changed files
with
221 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.Linq; | ||
using HandlebarsDotNet.Collections; | ||
using Xunit; | ||
|
||
namespace HandlebarsDotNet.Test.Collections | ||
{ | ||
public class WeakCollectionTests | ||
{ | ||
private readonly WeakCollection<object> _collection = new WeakCollection<object>(); | ||
|
||
[Fact] | ||
public void ObjectIsRemovedFromCollectionOnceNotReachable() | ||
{ | ||
var _0 = new object(); | ||
_collection.Add(_0); | ||
|
||
CallInItsOwnScope(() => | ||
{ | ||
var _1 = new object(); | ||
_collection.Add(_1); | ||
GC.Collect(); | ||
Assert.Equal(2, _collection.Size); | ||
Assert.Equal(2, _collection.Count()); | ||
}); | ||
|
||
GC.Collect(); | ||
Assert.Equal(2, _collection.Size); | ||
Assert.Single(_collection); | ||
} | ||
|
||
[Fact] | ||
public void BucketInCollectionReused() | ||
{ | ||
var _0 = new object(); | ||
_collection.Add(_0); | ||
|
||
CallInItsOwnScope(() => | ||
{ | ||
_collection.Add(new object()); | ||
_collection.Add(new object()); | ||
_collection.Add(new object()); | ||
}); | ||
|
||
GC.Collect(); | ||
|
||
_ = _collection.Count(); // force enumeration to set inner index | ||
|
||
var _2 = new object(); | ||
var _3 = new object(); | ||
_collection.Add(_2); | ||
_collection.Add(_3); | ||
|
||
GC.Collect(); | ||
Assert.Equal(4, _collection.Size); | ||
Assert.Equal(3, _collection.Count()); | ||
} | ||
|
||
[Fact] | ||
public void BucketInCollectionReusedAfterRemove() | ||
{ | ||
var _0 = new object(); | ||
var _1 = new object(); | ||
var _2 = new object(); | ||
_collection.Add(_0); | ||
_collection.Add(_1); | ||
_collection.Add(_2); | ||
|
||
GC.Collect(); | ||
|
||
_collection.Remove(_0); | ||
_collection.Remove(_1); | ||
_collection.Add(new object()); | ||
|
||
Assert.Equal(3, _collection.Size); | ||
Assert.Equal(2, _collection.Count()); | ||
} | ||
|
||
private static void CallInItsOwnScope(Action scope) => scope(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
namespace HandlebarsDotNet.Collections | ||
{ | ||
public class WeakCollection<T> : IEnumerable<T> where T : class | ||
{ | ||
private readonly List<WeakReference<T>> _store = new List<WeakReference<T>>(); | ||
private int _firstAvailableIndex = 0; | ||
|
||
public int Size => _store.Count; | ||
|
||
public void Add(T value) | ||
{ | ||
for (var index = _firstAvailableIndex; index < _store.Count; index++) | ||
{ | ||
if (_store[index] == null) | ||
{ | ||
_firstAvailableIndex = index + 1; | ||
_store[index] = new WeakReference<T>(value); | ||
return; | ||
} | ||
|
||
if (!_store[index].TryGetTarget(out _)) | ||
{ | ||
_firstAvailableIndex = index + 1; | ||
_store[index].SetTarget(value); | ||
return; | ||
} | ||
} | ||
|
||
_store.Add(new WeakReference<T>(value)); | ||
_firstAvailableIndex = _store.Count; | ||
} | ||
|
||
public void Remove(T value) | ||
{ | ||
for (var index = 0; index < _store.Count; index++) | ||
{ | ||
if (_store[index] == null) | ||
{ | ||
_firstAvailableIndex = Math.Min(_firstAvailableIndex, index); | ||
continue; | ||
} | ||
|
||
if (!_store[index].TryGetTarget(out var target)) | ||
{ | ||
_firstAvailableIndex = Math.Min(_firstAvailableIndex, index); | ||
continue; | ||
} | ||
|
||
if (target.Equals(value)) | ||
{ | ||
_store[index] = null; | ||
_firstAvailableIndex = Math.Min(_firstAvailableIndex, index); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
public IEnumerator<T> GetEnumerator() | ||
{ | ||
for (var index = 0; index < _store.Count; index++) | ||
{ | ||
var reference = _store[index]; | ||
if (reference == null) | ||
{ | ||
_firstAvailableIndex = Math.Min(_firstAvailableIndex, index); | ||
continue; | ||
} | ||
|
||
if (!reference.TryGetTarget(out var target)) | ||
{ | ||
_firstAvailableIndex = Math.Min(_firstAvailableIndex, index); | ||
continue; | ||
} | ||
|
||
yield return target; | ||
} | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.