클래스에 "Dispose"가 모든 이벤트를 unhooks하고 해당 객체를 참조하는 타이머를 삭제한다는 것을 확인하기위한 단위 테스트를 작성했습니다.WeakReference.IsLive()가 false가되기 전에 전체 가비지 수집 후에 지연이 있습니까?
그러나 가끔 WeakReference.IsLive()는 false를 반환 할 때 true를 반환합니까?
그래서 WeakReference.IsLive()가 업데이트되기 전에 전체 GC 후에 지연이 있습니까?
그렇지 않다면, 반복 할 수없는 결과를 줄 수있는 다른 것을 생각해 낼 수 있습니까?
WeakReference weekJobWatchDog = new WeakReference(jobWatchDog);
jobWatchDog = null;
// not collected before Dispose called due to timer and events etc
GC.Collect(); GC.Collect();
Assert.IsTrue(weekJobWatchDog.IsAlive);
((IDisposable)weekJobWatchDog.Target).Dispose();
// is now collected as Dispose unlocked all events and dispoed the timer
GC.Collect(); GC.Collect();
Assert.IsFalse(weekJobWatchDog.IsAlive); // sometimes this fails, about 1 in 4 runs
참조 관련 그러나 다른 질문도 Testing Finalizers and IDisposable.
How can I write a unit test to determine whether an object can be garbage collected?에는 GC.WaitForPendingFinalizers()를 호출하는 솔루션이 있지만 GC.WaitForPendingFinalizers()를 호출하지 않아야합니다. 따라서 처리 작업을 증명하고 싶은데, 파이널 라이저를 실행할 필요가 없습니다. .
WaitForPendingFinalizers를 호출 해 보셨습니까? – Henrik
@Henrik, 아니요, 우리가 기다리고 자하는 파이널 라이저를 가지고 있지 않기 때문에 –