|
| 1 | +using NUnit.Framework; |
| 2 | +using Python.Runtime; |
| 3 | +using System; |
| 4 | +using System.Threading; |
| 5 | + |
| 6 | +namespace Python.EmbeddingTest |
| 7 | +{ |
| 8 | + public class TestFinalizer |
| 9 | + { |
| 10 | + private string _PYTHONMALLOC = string.Empty; |
| 11 | + |
| 12 | + [SetUp] |
| 13 | + public void SetUp() |
| 14 | + { |
| 15 | + try |
| 16 | + { |
| 17 | + _PYTHONMALLOC = Environment.GetEnvironmentVariable("PYTHONMALLOC"); |
| 18 | + } |
| 19 | + catch (ArgumentNullException) |
| 20 | + { |
| 21 | + _PYTHONMALLOC = string.Empty; |
| 22 | + } |
| 23 | + Environment.SetEnvironmentVariable("PYTHONMALLOC", "malloc"); |
| 24 | + PythonEngine.Initialize(); |
| 25 | + } |
| 26 | + |
| 27 | + [TearDown] |
| 28 | + public void TearDown() |
| 29 | + { |
| 30 | + PythonEngine.Shutdown(); |
| 31 | + if (string.IsNullOrEmpty(_PYTHONMALLOC)) |
| 32 | + { |
| 33 | + Environment.SetEnvironmentVariable("PYTHONMALLOC", _PYTHONMALLOC); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private static void FullGCCollect() |
| 38 | + { |
| 39 | + GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); |
| 40 | + GC.WaitForFullGCComplete(); |
| 41 | + GC.WaitForPendingFinalizers(); |
| 42 | + } |
| 43 | + |
| 44 | + [Test] |
| 45 | + public void CollectBasicObject() |
| 46 | + { |
| 47 | + int thId = Thread.CurrentThread.ManagedThreadId; |
| 48 | + Finalizer.Instance.Threshold = 1; |
| 49 | + bool called = false; |
| 50 | + EventHandler<Finalizer.CollectArgs> handler = (s, e) => |
| 51 | + { |
| 52 | + Assert.AreEqual(thId, Thread.CurrentThread.ManagedThreadId); |
| 53 | + Assert.GreaterOrEqual(e.ObjectCount, 1); |
| 54 | + called = true; |
| 55 | + }; |
| 56 | + Finalizer.Instance.CollectOnce += handler; |
| 57 | + FullGCCollect(); |
| 58 | + PyLong obj = new PyLong(1024); |
| 59 | + |
| 60 | + WeakReference shortWeak = new WeakReference(obj); |
| 61 | + WeakReference longWeak = new WeakReference(obj, true); |
| 62 | + obj = null; |
| 63 | + FullGCCollect(); |
| 64 | + // The object has been resurrected |
| 65 | + Assert.IsFalse(shortWeak.IsAlive); |
| 66 | + Assert.IsTrue(longWeak.IsAlive); |
| 67 | + |
| 68 | + Assert.IsFalse(called); |
| 69 | + var garbage = Finalizer.Instance.GetCollectedObjects(); |
| 70 | + // FIXME: If make some query for garbage, |
| 71 | + // the above case will failed Assert.IsFalse(shortWeak.IsAlive) |
| 72 | + //Assert.IsTrue(garbage.All(T => T.IsAlive)); |
| 73 | + |
| 74 | + Finalizer.Instance.CallPendingFinalizers(); |
| 75 | + Assert.IsTrue(called); |
| 76 | + |
| 77 | + FullGCCollect(); |
| 78 | + //Assert.IsFalse(garbage.All(T => T.IsAlive)); |
| 79 | + |
| 80 | + Assert.IsNull(longWeak.Target); |
| 81 | + |
| 82 | + Finalizer.Instance.CollectOnce -= handler; |
| 83 | + } |
| 84 | + |
| 85 | + private static long CompareWithFinalizerOn(PyObject pyCollect, bool enbale) |
| 86 | + { |
| 87 | + // Must larger than 512 bytes make sure Python use |
| 88 | + string str = new string('1', 1024); |
| 89 | + Finalizer.Instance.Enable = true; |
| 90 | + FullGCCollect(); |
| 91 | + FullGCCollect(); |
| 92 | + pyCollect.Invoke(); |
| 93 | + Finalizer.Instance.CallPendingFinalizers(); |
| 94 | + Finalizer.Instance.Enable = enbale; |
| 95 | + |
| 96 | + // Estimate unmanaged memory size |
| 97 | + long before = Environment.WorkingSet - GC.GetTotalMemory(true); |
| 98 | + for (int i = 0; i < 10000; i++) |
| 99 | + { |
| 100 | + // Memory will leak when disable Finalizer |
| 101 | + new PyString(str); |
| 102 | + } |
| 103 | + FullGCCollect(); |
| 104 | + FullGCCollect(); |
| 105 | + pyCollect.Invoke(); |
| 106 | + if (enbale) |
| 107 | + { |
| 108 | + Finalizer.Instance.CallPendingFinalizers(); |
| 109 | + } |
| 110 | + |
| 111 | + FullGCCollect(); |
| 112 | + FullGCCollect(); |
| 113 | + long after = Environment.WorkingSet - GC.GetTotalMemory(true); |
| 114 | + return after - before; |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Because of two vms both have their memory manager, |
| 120 | + /// this test only prove the finalizer has take effect. |
| 121 | + /// </summary> |
| 122 | + [Test] |
| 123 | + [Ignore("Too many uncertainties, only manual on when debugging")] |
| 124 | + public void SimpleTestMemory() |
| 125 | + { |
| 126 | + bool oldState = Finalizer.Instance.Enable; |
| 127 | + try |
| 128 | + { |
| 129 | + using (PyObject gcModule = PythonEngine.ImportModule("gc")) |
| 130 | + using (PyObject pyCollect = gcModule.GetAttr("collect")) |
| 131 | + { |
| 132 | + long span1 = CompareWithFinalizerOn(pyCollect, false); |
| 133 | + long span2 = CompareWithFinalizerOn(pyCollect, true); |
| 134 | + Assert.Less(span2, span1); |
| 135 | + } |
| 136 | + } |
| 137 | + finally |
| 138 | + { |
| 139 | + Finalizer.Instance.Enable = oldState; |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments