/// <summary> /// Checks, is given memory address contain data, which can be recognized as .Net string by template: /// | sync=0 | mtdtbl=known | length | string with 'length' length | zero | /// TODO: test it on x64 and fix formulas /// </summary> private static unsafe bool IsString(long pointer) { var mtptr = pointer + IntPtr.Size; if (GCEx.PointsToAllocated((IntPtr)pointer) && *(IntPtr *)pointer == IntPtr.Zero && *(IntPtr *)mtptr == StringsTable) { if (GCEx.MajorNetVersion >= 4) { var length = *(int *)((int)pointer + 8); if (length < 2048 && *(short *)((int)pointer + 12 + length * 2) == 0) { return(true); } } else { var length = *(int *)((int)pointer + 12); if (length < 2048 && *(short *)((int)pointer + 14 + length * 2) == 0) { return(true); } } } return(false); }
private void MethodInsideAppDomain() { var startObj = new object(); ThreadPool.QueueUserWorkItem( delegate { // What for Console.ReadKey(). Thread.Sleep(1000); // Lookup our List'1, printing while looking up List <int> catched = null; foreach (var obj in GCEx.GetObjectsInSOH(startObj, mt => mt != 0)) { Console.WriteLine(" - object: {0}, type: {1}, size: {2}", obj.Item, obj.Item.GetType().Name, obj.Item.SizeOf()); var ints = obj.Item as List <int>; if (ints != null) { catched = ints; } } // Congrats if found if (catched != null) { Console.WriteLine("Catched list size: {0}", catched.Count); } } ); }
private void methodInsideAppDomain(IntPtr startingIntPtr) { foreach (var obj in GCEx.GetObjectsInSOH(EntityPtr.ToInstance <object>(startingIntPtr), mt => mt != 0)) { Console.WriteLine(" - object: {0}, type: {1}, size: {2}", obj.Item, obj.Item.GetType().Name, GCEx.SizeOf(obj.Item)); } }
public void DisposeOnFinalizeWorks() { var i = 0; DisposableEx.Action(() => i++).DisposeOnFinalize(); GCEx.CollectAndWait(); Assert.Equal(1, i); }
public void DelegateIsWeak() { var wd1 = WeakDelegate <Func <int> > .Create(); { var t = new SomeClass(5); wd1.Add(t.GetValue); Assert.Equal(5, wd1.Invoker()); } GCEx.CollectAndWait(); Assert.Equal(0, wd1.Invoker()); }
public void ClearWorksAfterCollect() { var wc = new WeakCollection <string>(); { var a = new string('a', 1); wc.Add(a); Assert.Equal("a", wc.First()); } GCEx.CollectAndWait(); wc.Clear(); Assert.Equal(0, wc.CapCount); Assert.False(wc.Any()); }
public void AddWorksAfterCollect() { var wc = new WeakCollection <string>(); { wc.Add(new string('a', 1)); Assert.Equal("a", wc.First()); } GCEx.CollectAndWait(); Assert.True(wc.CapCount <= 1); //Assert.Equal(0, wc.Count); wc.Add(new string('b', 1)); Assert.Equal(1, wc.Count); Assert.Equal("b", wc.First()); }
public void RemoveWorksAfterCollect() { var arr = new[] { "a", "b" }; var wc = new WeakCollection <string>(arr); { wc.Add(new string('c', 1)); wc.Add(new string('d', 1)); wc.Add(new string('c', 1)); } GCEx.CollectAndWait(); Assert.False(wc.Remove("c")); Assert.False(wc.Remove("d")); Assert.True(wc.SequenceEqual(new[] { "a", "b" })); }
public void KeepAliveTest() { WeakReference wr; { wr = new WeakReference(new object()); using (DisposableEx.Null().KeepAlive(wr.Target)) { GCEx.CollectAndWait(); Assert.True(wr.IsAlive); } } GCEx.CollectAndWait(); Assert.False(wr.IsAlive); }
public void CopyToWorksAfterCollect() { var arr = new[] { "a", "b" }; var wc = new WeakCollection <string>(arr); { wc.Add(new string('c', 1)); wc.Add(new string('d', 1)); } GCEx.CollectAndWait(); var target = new string[5]; wc.CopyTo(target, 1); Assert.True(arr.SequenceEqual(target.Skip(1).Take(2))); Assert.Null(target[0]); Assert.Null(target[3]); Assert.Null(target[4]); }
public void ContainsWorksAfterCollect() { var arr = new[] { "a", "b" }; var wc = new WeakCollection <string>(arr); { wc.Add(new string('c', 1)); wc.Add(new string('d', 1)); Assert.True(wc.Contains("a")); Assert.True(wc.Contains("b")); Assert.True(wc.Contains("c")); Assert.True(wc.Contains("d")); } GCEx.CollectAndWait(); Assert.True(wc.Contains("a")); Assert.True(wc.Contains("b")); Assert.False(wc.Contains("c")); Assert.False(wc.Contains("d")); Assert.False(wc.Contains("e")); }
public void DoesntHoldReferencesRemoveAll() { var wr1 = new WeakReference(new object()); var wr2 = new WeakReference(new object()); var c = new FastCollection<object> { wr1.Target, wr2.Target }; GCEx.CollectAndWait(); Assert.Equal(2, c.Count); Assert.True(wr1.IsAlive); Assert.True(wr2.IsAlive); c.RemoveAll(x => x == wr1.Target); GCEx.CollectAndWait(); Assert.Equal(1, c.Count); Assert.False(wr1.IsAlive); Assert.True(wr2.IsAlive); c.RemoveAll(x => x == wr2.Target); GCEx.CollectAndWait(); Assert.Equal(0, c.Count); Assert.False(wr1.IsAlive); Assert.False(wr2.IsAlive); }
public static void DoesntHoldReferences() { var wr1 = new WeakReference(new object()); var wr2 = new WeakReference(new object()); var c = new FastStack <object>(); c.Push(wr1.Target); c.Push(wr2.Target); GCEx.CollectAndWait(); Assert.Equal(2, c.Count); Assert.True(wr1.IsAlive); Assert.True(wr2.IsAlive); c.Pop(); GCEx.CollectAndWait(); Assert.Equal(1, c.Count); Assert.True(wr1.IsAlive); Assert.False(wr2.IsAlive); c.Pop(); GCEx.CollectAndWait(); Assert.Equal(0, c.Count); Assert.False(wr1.IsAlive); Assert.False(wr2.IsAlive); }
private static void PrintSize <T>(T val) { Console.WriteLine("sizeof({0}): {1} bytes ({2})", typeof(T).Name, GCEx.SizeOf(val), val); }
private static void PrintSize <T>() { Console.WriteLine("sizeof({0}): {1} bytes", typeof(T).Name, GCEx.SizeOf <T>()); }
private static unsafe void Main() { var objects = new Dictionary <Type, int>(7000); // Get current heap ranges IntPtr managedStart, managedEnd; Console.ReadKey(); MemAccessor.GetManagedHeap(out managedStart, out managedEnd); // for each byte in virtual memory block, we trying to find strings var stopwatch = Stopwatch.StartNew(); for (IntPtr *ptr = (IntPtr *)managedStart, end = (IntPtr *)managedEnd; ptr < end; ptr++) { if (IsCorrectMethodsTable(*ptr)) { // checking next object. int size; try { size = GCEx.SizeOf((EntityInfo *)(ptr - 1)) >> 2; } catch (OverflowException) { continue; } if (ptr + size > (long *)managedEnd) { continue; } { var found = EntityPtr.ToInstance <object>((IntPtr)(ptr - 1)); RegisterObject(objects, found); var lastInChain = found; foreach (var item in GCEx.GetObjectsInSOH(found, hmt => IsCorrectMethodsTable((IntPtr)hmt))) { RegisterObject(objects, item.Item); if (!item.IsArrayItem) { lastInChain = item.Item; } } long lastRecognized = (long)EntityPtr.ToPointer(lastInChain); ptr = (IntPtr *)(lastRecognized + lastInChain.SizeOf()); } } } var timeToTakeSnapshot = stopwatch.ElapsedMilliseconds; foreach (var type in objects.Keys.OrderByDescending(key => objects[key])) { Console.WriteLine("{0:00000} : {1}", objects[type], type.FullName); } Console.WriteLine("Objects total: {0}. Time taken: {1}", objects.Values.Sum(), timeToTakeSnapshot); Console.ReadKey(); }