public static void WaitForObjects() { var list = _trackers.SelectMany(kvp => kvp.Value.Entries.Select(e => (e, kvp.Key))).ToArray(); _trackers.Clear(); // snapshot a list of objects created so far // some of them might be from concurrent tests // var list = new List<WeakReference>(ObjectTracker.Default.Items.Select(x => new WeakReference(x))); try { Console.WriteLine($"Cleaning {list.Count()} objects..."); WaitFor(() => { GC.Collect(); GC.WaitForPendingFinalizers(); return(list.All(x => x.e.WeakReference.Target == null)); // return ObjectTracker.Default.Count == 0; }); Console.WriteLine("All objects are clear"); } catch { var objs = list.Where(x => x.e.WeakReference.Target != null).ToArray(); // var objs = ObjectTracker.Default.Items.Where(x => x != null).ToArray(); Console.WriteLine($"Objects alive: {objs.Length} ======================"); foreach (var item in objs) { Console.WriteLine($"{item.Key}: {item.e.Type.Name}: {Stringify.ToString(item.e.WeakReference.Target, true)}"); } throw; } }
static void Main(string[] args) { if (!args.Any()) { MainClient(); return; } RiverInit.RegAll(); _timer = new Timer(Tick, null, 1000, 1000); // ObjectTracker.TypesToPrint.Add(typeof(Handler)); var server1 = new SocksServer { Chain = { // "ss://*****:*****@127.0.0.1:8338", }, }; server1.Run("socks://0.0.0.0:1080"); /* * var server2 = new ShadowSocksServer * { * Chain = * { * // "ss://*****:*****@127.0.0.1:8338", * }, * }; * server2.Run("ss://*****:*****@0.0.0.0:8338"); */ string q; do { q = Console.ReadLine(); GC.Collect(); GC.WaitForPendingFinalizers(); GC.WaitForFullGCApproach(); foreach (var item in ObjectTracker.Default.Get <Thread>()) { Console.WriteLine(Stringify.ToString(item, true)); } } while (q != "q"); }