public void InsertStress() { if (GC.MaxGeneration == 0) /*Boehm doesn't handle ephemerons */ { Assert.Ignore("Not working on Boehm."); } var cwt = new ConditionalWeakTable <object, object> (); var a = new object(); var b = new object(); cwt.Add(a, new object()); cwt.Add(b, new object()); List <WeakReference> res = null; FinalizerHelpers.PerformNoPinAction(() => { res = FillWithNetwork(cwt); }); GC.Collect(); GC.Collect(); for (int i = 0; i < res.Count; ++i) { Assert.IsFalse(res [i].IsAlive, "#r" + i); } }
public void TestCacheCompilationTwice() { ObjectReference <CSharpCompilation> weak1 = null, weak3 = null; ProjectCacheService cache = null; FinalizerHelpers.PerformNoPinAction(() => { var comp1 = CSharpCompilation.Create("1"); var comp2 = CSharpCompilation.Create("2"); var comp3 = CSharpCompilation.Create("3"); weak3 = ObjectReference.Create(comp3); weak1 = ObjectReference.Create(comp1); var workspace = new AdhocWorkspace(MockHostServices.Instance, workspaceKind: WorkspaceKind.Host); cache = new ProjectCacheService(workspace, int.MaxValue); var key = ProjectId.CreateNewId(); var owner = new object(); cache.CacheObjectIfCachingEnabledForKey(key, owner, comp1); cache.CacheObjectIfCachingEnabledForKey(key, owner, comp2); cache.CacheObjectIfCachingEnabledForKey(key, owner, comp3); // When we cache 3 again, 1 should stay in the cache cache.CacheObjectIfCachingEnabledForKey(key, owner, comp3); }); weak3.AssertHeld(); weak1.AssertHeld(); GC.KeepAlive(cache); }
public void Reachability() { if (GC.MaxGeneration == 0) /*Boehm doesn't handle ephemerons */ { Assert.Ignore("Not working on Boehm."); } var cwt = new ConditionalWeakTable <object, object> (); List <object> keepAlive = null; List <WeakReference> keys = null; FinalizerHelpers.PerformNoPinAction(delegate() { FillStuff(cwt, out keepAlive, out keys); }); GC.Collect(); Assert.IsTrue(keys [0].IsAlive, "r0"); Assert.IsFalse(keys [1].IsAlive, "r1"); Assert.IsTrue(keys [2].IsAlive, "r2"); object res; Assert.IsTrue(cwt.TryGetValue(keepAlive [0], out res), "ka0"); Assert.IsTrue(res is Link, "ka1"); Link link = res as Link; Assert.IsTrue(cwt.TryGetValue(link.obj, out res), "ka2"); Assert.AreEqual("str0", res, "ka3"); }
public void TestEjectFromImplicitCache() { ProjectCacheService cache = null; ObjectReference <Compilation> weakFirst = null, weakLast = null; FinalizerHelpers.PerformNoPinAction(() => { int total = ProjectCacheService.ImplicitCacheSize + 1; var compilations = new Compilation [total]; for (int i = 0; i < total; i++) { compilations [i] = CSharpCompilation.Create(i.ToString()); } weakFirst = ObjectReference.Create(compilations [0]); weakLast = ObjectReference.Create(compilations [total - 1]); var workspace = new AdhocWorkspace(MockHostServices.Instance, workspaceKind: WorkspaceKind.Host); cache = new ProjectCacheService(workspace, int.MaxValue); for (int i = 0; i < total; i++) { cache.CacheObjectIfCachingEnabledForKey(ProjectId.CreateNewId(), (object)null, compilations [i]); } }); weakFirst.AssertReleased(); weakLast.AssertHeld(); GC.KeepAlive(cache); }
static int test_0_CWT_keep_child_alive() { Console.WriteLine("test_0_CWT_keep_child_alive"); FinalizerHelpers.PerformNoPinAction(SetupLinks3); GC.Collect(); GC.WaitForPendingFinalizers(); Console.WriteLine("try get A {0}", root.TryGetTarget(out a)); Console.WriteLine("try get B {0}", child.TryGetTarget(out b)); Console.WriteLine("a is null {0}", a == null); Console.WriteLine("b is null {0}", b == null); if (a == null || b != null) { return(1); } Console.WriteLine("a test {0}", a.__test); Console.WriteLine("try get a_val {0}", root_value.TryGetTarget(out a_val)); Console.WriteLine("try get v_val {0}", child_value.TryGetTarget(out b_val)); //the strong toggleref must keep the CWT value to remains alive if (a_val == null) { return(2); } //the weak toggleref should allow the CWT value to go away if (b_val != null) { return(3); } object res_value = null; bool res = cwt.TryGetValue(a, out res_value); Console.WriteLine("CWT result {0} -> {1}", res, res_value == a_val); //the strong val is not on the CWT if (!res) { return(4); } //for some reason the value is not the right one if (res_value != a_val) { return(5); } return(0); }
public void WeakTest() { //Finalizable.debug = true; var t = new Test(); FinalizerHelpers.PerformNoPinAction(delegate() { FinalizerHelpers.PerformNoPinAction(delegate() { t.Obj = new Finalizable(); t.Obj2 = new Finalizable(); t.Obj3 = new Finalizable(); t.Obj4 = Test.retain = new Finalizable(); Test.retain.a = 0x1029458; }); GC.Collect(0); GC.Collect(); GC.WaitForPendingFinalizers(); GC.WaitForPendingFinalizers(); Assert.That(t.Obj, Is.Null, "'t.Obj' should be null"); Assert.That(t.Obj2, Is.Null, "'t.Obj2' should be null"); Assert.That(t.Obj3, Is.Not.Null, "'t.Obj3' should not be null"); //overflow the nursery, make sure we fill it #if __WATCHOS__ for (int i = 0; i < 1000 * 100; ++i) // the apple watch doesn't have much memory, so try to not run into OOMs either. The nursery is 512k, so 100k objects should be more than enough to fill it. #else for (int i = 0; i < 1000 * 1000 * 10; ++i) #endif { new OneField(); } Exception ex = null; FinalizerHelpers.PerformNoPinAction(delegate() { try { // This must be done on a separate thread so that the 'Test.retain' value doesn't // show up on the main thread's stack as a temporary value in registers the // GC can see. Assert.That(Test.retain.a, Is.EqualTo(0x1029458), "retain.a"); } catch (Exception e) { ex = e; } }); Test.retain = null; }); GC.Collect(); GC.WaitForPendingFinalizers(); GC.WaitForPendingFinalizers(); Assert.That(t.Obj4, Is.Null, "'t.Obj4' should be null"); }
public static int Main(String[] args) { var t = new Tests(); FinalizerHelpers.PerformNoPinAction(delegate() { FinalizerHelpers.PerformNoPinAction(delegate() { t.Obj = new Finalizable(); t.Obj2 = new Finalizable(); t.Obj3 = new Finalizable(); t.Obj4 = retain = new Finalizable(); retain.a = 0x1029458; }); GC.Collect(0); GC.Collect(); GC.WaitForPendingFinalizers(); if (t.Obj != null) { Environment.Exit(1); } if (t.Obj2 != null) { Environment.Exit(2); } if (t.Obj3 == null) { Environment.Exit(3); } //overflow the nursery, make sure we fill it for (int i = 0; i < 1000 * 1000 * 10; ++i) { new OneField(); } if (retain.a != 0x1029458) { Environment.Exit(4); } retain = null; }); GC.Collect(); GC.WaitForPendingFinalizers(); if (t.Obj4 != null) { return(5); } return(0); }
public void ReRegisterForFinalizeTest() { FinalizerHelpers.PerformNoPinAction(delegate() { Run_ReRegisterForFinalizeTest(); }); var t = Task.Factory.StartNew(() => { do { GC.Collect(); GC.WaitForPendingFinalizers(); Task.Yield(); } while (MyFinalizeObject.finalized != 2); }); Assert.IsTrue(t.Wait(5000)); }
static int test_0_root_keeps_child() { Console.WriteLine("test_0_root_keeps_child"); FinalizerHelpers.PerformNoPinAction(SetupLinks); GC.Collect(); GC.WaitForPendingFinalizers(); Console.WriteLine("try get A {0}", root.TryGetTarget(out a)); Console.WriteLine("try get B {0}", child.TryGetTarget(out b)); Console.WriteLine("a is null {0}", a == null); Console.WriteLine("b is null {0}", b == null); if (a == null || b == null) { return(1); } Console.WriteLine("a test {0}", a.__test); Console.WriteLine("b test {0}", b.__test); //now we break the link and switch b to strong a.link.Clear(); b.__test = Toggleref.STRONG; a = b = null; GC.Collect(); GC.WaitForPendingFinalizers(); Console.WriteLine("try get A {0}", root.TryGetTarget(out a)); Console.WriteLine("try get B {0}", child.TryGetTarget(out b)); Console.WriteLine("a is null {0}", a == null); Console.WriteLine("b is null {0}", b == null); if (a == null || b == null) { return(2); } Console.WriteLine("a test {0}", a.__test); Console.WriteLine("b test {0}", b.__test); return(0); }
public void FinalizableObjectsThatRetainDeadKeys() { if (GC.MaxGeneration == 0) /*Boehm doesn't handle ephemerons */ { Assert.Ignore("Not working on Boehm."); } lock (_lock1) { var cwt = new ConditionalWeakTable <object, object> (); FinalizerHelpers.PerformNoPinAction(() => { FillWithFinalizable(cwt); }); GC.Collect(); GC.Collect(); Assert.AreEqual(0, reachable, "#1"); } GC.Collect(); GC.Collect(); lock (_lock2) { Monitor.Wait(_lock2, 1000); } Assert.AreEqual(20, reachable, "#1"); }
static int test_0_child_goes_away() { Console.WriteLine("test_0_child_goes_away"); FinalizerHelpers.PerformNoPinAction(SetupLinks2); GC.Collect(); GC.WaitForPendingFinalizers(); Console.WriteLine("try get A {0}", root.TryGetTarget(out a)); Console.WriteLine("try get B {0}", child.TryGetTarget(out b)); Console.WriteLine("a is null {0}", a == null); Console.WriteLine("b is null {0}", b == null); if (a == null || b != null) { return(1); } Console.WriteLine("a test {0}", a.__test); return(0); }
public void OldGenStress() { if (GC.MaxGeneration == 0) /*Boehm doesn't handle ephemerons */ { Assert.Ignore("Not working on Boehm."); } var cwt = new ConditionalWeakTable <object, object> [1]; List <object> k = null; List <WeakReference> res, res2; res = res2 = null; FinalizerHelpers.PerformNoPinAction(() => { res = FillWithNetwork2(cwt); ForcePromotion(); k = FillReachable(cwt); res2 = FillWithNetwork2(cwt); }); GC.Collect(); for (int i = 0; i < res.Count; ++i) { Assert.IsFalse(res [i].IsAlive, "#r0-" + i); } for (int i = 0; i < res2.Count; ++i) { Assert.IsFalse(res2 [i].IsAlive, "#r1-" + i); } for (int i = 0; i < k.Count; ++i) { object val; Assert.IsTrue(cwt[0].TryGetValue(k [i], out val), "k0-" + i); Assert.AreEqual(i, val, "k1-" + i); } }