Last active
March 6, 2024 13:32
-
-
Save myroot/30fa5cb9dd69a3bfee607b2721b468e0 to your computer and use it in GitHub Desktop.
C# Delegate와 HashSet 그리고 WeakReference
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
using System; | |
class Program { | |
delegate void MyDelegate(int v); | |
HashSet<object> _set = new(); | |
WeakReference<MyDelegate> _weak; | |
public void Test() | |
{ | |
MyDelegate d1 = (MyDelegate)Fun; | |
if (_weak != null && _weak.TryGetTarget(out var target)) { | |
Console.WriteLine($"d1 == target : {target == d1}"); | |
Console.WriteLine($"Object.ReferenceEquals {Object.ReferenceEquals(d1, target)}"); | |
Console.WriteLine($"d1.GetHashCode() == target.GetHashCode() : {d1.GetHashCode() == target.GetHashCode()}"); | |
} | |
// if (d1 is Delegate) { | |
// if (_set.Contains(d1)){ | |
// _set.Remove(d1); | |
// } | |
// } | |
_set.Add(d1); | |
_weak = new WeakReference<MyDelegate>(d1); | |
Console.WriteLine($"--------------- set size : {_set.Count}"); | |
GC.Collect(); | |
GC.Collect(); | |
GC.Collect(); | |
} | |
public void CheckWeak(){ | |
if (_weak != null && _weak.TryGetTarget(out var target)) { | |
Console.WriteLine($"Weak alive: {target}"); | |
} | |
else { | |
Console.WriteLine($"Weak dead"); | |
} | |
} | |
public void GarbageAndGC() { | |
for (int i = 0 ; i <10000; i++) { | |
byte[] b = new byte[1000]; | |
} | |
GC.Collect(); | |
GC.Collect(); | |
} | |
public void Fun(int v) {} | |
public static void Main(string[] args) { | |
Console.WriteLine("Hello world"); | |
var p = new Program(); | |
p.Test(); | |
p.GarbageAndGC(); | |
p.CheckWeak(); | |
p.Test(); | |
p.GarbageAndGC(); | |
p.CheckWeak(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
당황 포인트
그런데 둘다 아니네
매번 새로운 Delegate객체가 생성되고, Delegate lifetime은 원래 instance method의 life time과 관계가 없음
반대로 Delegate가 instance method의 this객체를 Target으로 저장하기에 instance method의 instance(this)가 Delegate에 의해서 lifetime이 expand될 수 있다.