Пример #1
0
        private static IEnumerable <MemoryTracker.ChildReference> DistillChildReferencesFromObject(object current, object parent, string currentPath)
        {
            Type type = current.GetType();

            if (type.IsClass)
            {
                yield return(new MemoryTracker.ChildReference
                {
                    child = current,
                    path = currentPath
                });
            }
            else if (!type.IsPrimitive)
            {
                if (!type.IsValueType)
                {
                    throw new NotImplementedException();
                }
                string structPath = currentPath + ".";
                foreach (MemoryTracker.ChildReference childReference in MemoryTracker.GetAllReferencedClassesFromClassOrStruct(current, MemoryTracker.GetFieldsFromHierarchy(type, BindingFlags.Instance), parent, structPath))
                {
                    yield return(childReference);
                }
            }
        }
Пример #2
0
 public static void LogObjectsLoaded()
 {
     if (MemoryTracker.tracked.Count == 0)
     {
         Log.Message("No objects tracked, memory tracker markup may not be applied.");
         return;
     }
     GC.Collect();
     MemoryTracker.LockTracking();
     try
     {
         foreach (HashSet <WeakReference> current in MemoryTracker.tracked.Values)
         {
             MemoryTracker.CullNulls(current);
         }
         StringBuilder stringBuilder = new StringBuilder();
         foreach (KeyValuePair <Type, HashSet <WeakReference> > current2 in from kvp in MemoryTracker.tracked
                  orderby - kvp.Value.Count
                  select kvp)
         {
             stringBuilder.AppendLine(string.Format("{0,6} {1}", current2.Value.Count, current2.Key));
         }
         Log.Message(stringBuilder.ToString());
     }
     finally
     {
         MemoryTracker.UnlockTracking();
     }
 }
Пример #3
0
 private static IEnumerable <MemoryTracker.ChildReference> GetAllReferencedClassesFromClassOrStruct(object current, IEnumerable <FieldInfo> fields, object parent, string currentPath)
 {
     foreach (FieldInfo field in fields)
     {
         if (!field.FieldType.IsPrimitive)
         {
             object referenced = null;
             referenced = field.GetValue(current);
             if (referenced != null)
             {
                 foreach (MemoryTracker.ChildReference child in MemoryTracker.DistillChildReferencesFromObject(referenced, parent, currentPath + field.Name))
                 {
                     yield return(child);
                 }
             }
         }
     }
     if (current != null && current is ICollection)
     {
         MemoryTracker.foundCollections.Add(new WeakReference(current));
         foreach (object entry in (current as IEnumerable))
         {
             if (entry != null && !entry.GetType().IsPrimitive)
             {
                 foreach (MemoryTracker.ChildReference child2 in MemoryTracker.DistillChildReferencesFromObject(entry, parent, currentPath + "[]"))
                 {
                     yield return(child2);
                 }
             }
         }
     }
 }
Пример #4
0
 private static void ObjectsLoaded()
 {
     if (MemoryTracker.tracked.Count == 0)
     {
         Log.Message("No objects tracked, memory tracker markup may not be applied.", false);
     }
     else
     {
         GC.Collect();
         MemoryTracker.LockTracking();
         try
         {
             foreach (HashSet <WeakReference> table in MemoryTracker.tracked.Values)
             {
                 MemoryTracker.CullNulls(table);
             }
             StringBuilder stringBuilder = new StringBuilder();
             foreach (KeyValuePair <Type, HashSet <WeakReference> > keyValuePair in from kvp in MemoryTracker.tracked
                      orderby - kvp.Value.Count
                      select kvp)
             {
                 stringBuilder.AppendLine(string.Format("{0,6} {1}", keyValuePair.Value.Count, keyValuePair.Key));
             }
             Log.Message(stringBuilder.ToString(), false);
         }
         finally
         {
             MemoryTracker.UnlockTracking();
         }
     }
 }
        private static IEnumerable <ChildReference> GetAllReferencedClassesFromClassOrStruct(object current, IEnumerable <FieldInfo> fields, object parent, string currentPath)
        {
            foreach (FieldInfo field in fields)
            {
                if (!field.FieldType.IsPrimitive)
                {
                    object referenced = field.GetValue(current);
                    if (referenced != null)
                    {
                        using (IEnumerator <ChildReference> enumerator2 = MemoryTracker.DistillChildReferencesFromObject(referenced, parent, currentPath + field.Name).GetEnumerator())
                        {
                            if (enumerator2.MoveNext())
                            {
                                ChildReference child2 = enumerator2.Current;
                                yield return(child2);

                                /*Error: Unable to find new state assignment for yield return*/;
                            }
                        }
                    }
                }
            }
            if (current != null && current is ICollection)
            {
                MemoryTracker.foundCollections.Add(new WeakReference(current));
                IEnumerator enumerator3 = (current as IEnumerable).GetEnumerator();
                try
                {
                    while (enumerator3.MoveNext())
                    {
                        object entry = enumerator3.Current;
                        if (entry != null && !entry.GetType().IsPrimitive)
                        {
                            using (IEnumerator <ChildReference> enumerator4 = MemoryTracker.DistillChildReferencesFromObject(entry, parent, currentPath + "[]").GetEnumerator())
                            {
                                if (enumerator4.MoveNext())
                                {
                                    ChildReference child = enumerator4.Current;
                                    yield return(child);

                                    /*Error: Unable to find new state assignment for yield return*/;
                                }
                            }
                        }
                    }
                }
                finally
                {
                    IDisposable disposable;
                    IDisposable disposable2 = disposable = (enumerator3 as IDisposable);
                    if (disposable != null)
                    {
                        disposable2.Dispose();
                    }
                }
            }
            yield break;
IL_02f3:
            /*Error near IL_02f4: Unexpected return in MoveNext()*/;
        }
Пример #6
0
 private static IEnumerable <FieldInfo> GetFieldsFromHierarchy(Type type, BindingFlags bindingFlags)
 {
     while (type != null)
     {
         foreach (FieldInfo field in MemoryTracker.GetFields(type, bindingFlags))
         {
             yield return(field);
         }
         type = type.BaseType;
     }
 }
Пример #7
0
        private static void CalculateReferencePaths(Dictionary <object, MemoryTracker.ReferenceData> references, IEnumerable <object> objects, int pathCost)
        {
            Queue <object> queue = new Queue <object>(objects);

            while (queue.Count > 0)
            {
                object obj = queue.Dequeue();
                if (references[obj].path.NullOrEmpty())
                {
                    references[obj].path     = string.Format("???.{0}", obj.GetType());
                    references[obj].pathCost = pathCost;
                }
                MemoryTracker.CalculateObjectReferencePath(obj, references, queue);
            }
        }
 public static void Update()
 {
     if (MemoryTracker.tracked.Count != 0 && MemoryTracker.updatesSinceLastCull++ >= 10)
     {
         MemoryTracker.updatesSinceLastCull = 0;
         KeyValuePair <Type, HashSet <WeakReference> > keyValuePair = MemoryTracker.tracked.ElementAtOrDefault(MemoryTracker.cullTargetIndex++);
         if (keyValuePair.Value == null)
         {
             MemoryTracker.cullTargetIndex = 0;
         }
         else
         {
             MemoryTracker.CullNulls(keyValuePair.Value);
         }
     }
 }
Пример #9
0
 public static void LogGrownCollections()
 {
     if (!MemoryTracker.AnythingTracked)
     {
         Log.Message("No objects tracked, memory tracker markup may not be applied.");
         return;
     }
     CollectionsTracker.collections.RemoveAll((KeyValuePair <WeakReference, int> kvp) => !kvp.Key.IsAlive || ((ICollection)kvp.Key.Target).Count <= kvp.Value);
     MemoryTracker.LogObjectHoldPathsFor(from kvp in CollectionsTracker.collections
                                         select kvp.Key, delegate(WeakReference elem)
     {
         ICollection collection = elem.Target as ICollection;
         return(collection.Count - CollectionsTracker.collections[elem]);
     });
     CollectionsTracker.collections.Clear();
 }
Пример #10
0
 private static IEnumerable <MemoryTracker.ChildReference> GetAllReferencedClassesFromClassOrStruct(object current, IEnumerable <FieldInfo> fields, object parent, string currentPath)
 {
     foreach (FieldInfo field in fields)
     {
         if (!field.FieldType.IsPrimitive)
         {
             object referenced = null;
             referenced = field.GetValue(current);
             if (referenced != null)
             {
                 foreach (MemoryTracker.ChildReference child in MemoryTracker.DistillChildReferencesFromObject(referenced, parent, currentPath + field.Name))
                 {
                     yield return(child);
                 }
             }
         }
     }
     if (current != null && current is ICollection)
     {
         MemoryTracker.foundCollections.Add(new WeakReference(current));
         IEnumerator enumerator3 = (current as IEnumerable).GetEnumerator();
         try
         {
             while (enumerator3.MoveNext())
             {
                 object entry = enumerator3.Current;
                 if (entry != null && !entry.GetType().IsPrimitive)
                 {
                     foreach (MemoryTracker.ChildReference child2 in MemoryTracker.DistillChildReferencesFromObject(entry, parent, currentPath + "[]"))
                     {
                         yield return(child2);
                     }
                 }
             }
         }
         finally
         {
             IDisposable disposable;
             if ((disposable = (enumerator3 as IDisposable)) != null)
             {
                 disposable.Dispose();
             }
         }
     }
     yield break;
 }
Пример #11
0
 private static void ObjectHoldPaths()
 {
     if (MemoryTracker.tracked.Count == 0)
     {
         Log.Message("No objects tracked, memory tracker markup may not be applied.", false);
     }
     else
     {
         GC.Collect();
         MemoryTracker.LockTracking();
         try
         {
             foreach (HashSet <WeakReference> table in MemoryTracker.tracked.Values)
             {
                 MemoryTracker.CullNulls(table);
             }
             List <Type> list = new List <Type>();
             list.Add(typeof(Map));
             List <FloatMenuOption> list2 = new List <FloatMenuOption>();
             foreach (Type type in list.Concat(from kvp in MemoryTracker.tracked
                                               orderby - kvp.Value.Count
                                               select kvp.Key).Take(30))
             {
                 Type type2 = type;
                 HashSet <WeakReference> trackedBatch = MemoryTracker.tracked.TryGetValue(type2, null);
                 if (trackedBatch == null)
                 {
                     trackedBatch = new HashSet <WeakReference>();
                 }
                 list2.Add(new FloatMenuOption(string.Format("{0} ({1})", type2, trackedBatch.Count), delegate()
                 {
                     MemoryTracker.LogObjectHoldPathsFor(trackedBatch, (WeakReference _) => 1);
                 }, MenuOptionPriority.Default, null, null, 0f, null, null));
                 if (list2.Count == 30)
                 {
                     break;
                 }
             }
             Find.WindowStack.Add(new FloatMenu(list2));
         }
         finally
         {
             MemoryTracker.UnlockTracking();
         }
     }
 }
Пример #12
0
 private static void UnlockTracking()
 {
     if (!MemoryTracker.trackedLocked)
     {
         throw new NotImplementedException();
     }
     MemoryTracker.trackedLocked = false;
     foreach (object current in MemoryTracker.trackedQueue)
     {
         MemoryTracker.RegisterObject(current);
     }
     MemoryTracker.trackedQueue.Clear();
     foreach (RuntimeTypeHandle current2 in MemoryTracker.trackedTypeQueue)
     {
         MemoryTracker.RegisterType(current2);
     }
     MemoryTracker.trackedTypeQueue.Clear();
 }
Пример #13
0
 public static void Update()
 {
     if (MemoryTracker.tracked.Count != 0)
     {
         if (MemoryTracker.updatesSinceLastCull++ >= 10)
         {
             MemoryTracker.updatesSinceLastCull = 0;
             HashSet <WeakReference> value = MemoryTracker.tracked.ElementAtOrDefault(MemoryTracker.cullTargetIndex++).Value;
             if (value == null)
             {
                 MemoryTracker.cullTargetIndex = 0;
             }
             else
             {
                 MemoryTracker.CullNulls(value);
             }
         }
     }
 }
Пример #14
0
        private static IEnumerable <FieldInfo> GetFieldsFromHierarchy(Type type, BindingFlags bindingFlags)
        {
            while (type != null)
            {
                using (IEnumerator <FieldInfo> enumerator = MemoryTracker.GetFields(type, bindingFlags).GetEnumerator())
                {
                    if (enumerator.MoveNext())
                    {
                        FieldInfo field = enumerator.Current;
                        yield return(field);

                        /*Error: Unable to find new state assignment for yield return*/;
                    }
                }
                type = type.BaseType;
            }
            yield break;
IL_00e0:
            /*Error near IL_00e1: Unexpected return in MoveNext()*/;
        }
Пример #15
0
        private static void AccumulateReferences(object obj, Dictionary <object, ReferenceData> references, HashSet <object> seen, Queue <object> toProcess)
        {
            ReferenceData referenceData = null;

            if (!references.TryGetValue(obj, out referenceData))
            {
                referenceData = (references[obj] = new ReferenceData());
            }
            foreach (ChildReference item in MemoryTracker.GetAllReferencedClassesFromClassOrStruct(obj, MemoryTracker.GetFieldsFromHierarchy(obj.GetType(), BindingFlags.Instance), obj, string.Empty))
            {
                ChildReference current = item;
                if (!current.child.GetType().IsClass)
                {
                    throw new ApplicationException();
                }
                ReferenceData referenceData3 = null;
                if (!references.TryGetValue(current.child, out referenceData3))
                {
                    referenceData3            = new ReferenceData();
                    references[current.child] = referenceData3;
                }
                referenceData3.referredBy.Add(new ReferenceData.Link
                {
                    target    = obj,
                    targetRef = referenceData,
                    path      = current.path
                });
                referenceData.refers.Add(new ReferenceData.Link
                {
                    target    = current.child,
                    targetRef = referenceData3,
                    path      = current.path
                });
                if (!seen.Contains(current.child))
                {
                    seen.Add(current.child);
                    toProcess.Enqueue(current.child);
                }
            }
        }
Пример #16
0
 public static void LogObjectHoldPaths()
 {
     if (MemoryTracker.tracked.Count == 0)
     {
         Log.Message("No objects tracked, memory tracker markup may not be applied.");
     }
     else
     {
         GC.Collect();
         MemoryTracker.LockTracking();
         try
         {
             foreach (HashSet <WeakReference> value in MemoryTracker.tracked.Values)
             {
                 MemoryTracker.CullNulls(value);
             }
             List <FloatMenuOption> list = new List <FloatMenuOption>();
             foreach (KeyValuePair <Type, HashSet <WeakReference> > item in from kvp in MemoryTracker.tracked
                      orderby - kvp.Value.Count
                      select kvp)
             {
                 KeyValuePair <Type, HashSet <WeakReference> > elementLocal = item;
                 list.Add(new FloatMenuOption(string.Format("{0} ({1})", item.Key, item.Value.Count), delegate
                 {
                     MemoryTracker.LogObjectHoldPathsFor(elementLocal.Value, (WeakReference _) => 1);
                 }, MenuOptionPriority.Default, null, null, 0f, null, null));
                 if (list.Count == 30)
                 {
                     break;
                 }
             }
             Find.WindowStack.Add(new FloatMenu(list));
         }
         finally
         {
             MemoryTracker.UnlockTracking();
         }
     }
 }
Пример #17
0
 private static void AccumulateReferences(object obj, Dictionary <object, MemoryTracker.ReferenceData> references, HashSet <object> seen, Queue <object> toProcess)
 {
     MemoryTracker.ReferenceData referenceData = null;
     if (!references.TryGetValue(obj, out referenceData))
     {
         referenceData   = new MemoryTracker.ReferenceData();
         references[obj] = referenceData;
     }
     foreach (MemoryTracker.ChildReference childReference in MemoryTracker.GetAllReferencedClassesFromClassOrStruct(obj, MemoryTracker.GetFieldsFromHierarchy(obj.GetType(), BindingFlags.Instance), obj, ""))
     {
         if (!childReference.child.GetType().IsClass)
         {
             throw new ApplicationException();
         }
         MemoryTracker.ReferenceData referenceData2 = null;
         if (!references.TryGetValue(childReference.child, out referenceData2))
         {
             referenceData2 = new MemoryTracker.ReferenceData();
             references[childReference.child] = referenceData2;
         }
         referenceData2.referredBy.Add(new MemoryTracker.ReferenceData.Link
         {
             target    = obj,
             targetRef = referenceData,
             path      = childReference.path
         });
         referenceData.refers.Add(new MemoryTracker.ReferenceData.Link
         {
             target    = childReference.child,
             targetRef = referenceData2,
             path      = childReference.path
         });
         if (!seen.Contains(childReference.child))
         {
             seen.Add(childReference.child);
             toProcess.Enqueue(childReference.child);
         }
     }
 }
Пример #18
0
 private static void AccumulateStaticMembers(Type type, Dictionary <object, MemoryTracker.ReferenceData> references, HashSet <object> seen, Queue <object> toProcess)
 {
     foreach (MemoryTracker.ChildReference current in MemoryTracker.GetAllReferencedClassesFromClassOrStruct(null, MemoryTracker.GetFields(type, BindingFlags.Static), null, type.ToString() + "."))
     {
         if (!current.child.GetType().IsClass)
         {
             throw new ApplicationException();
         }
         MemoryTracker.ReferenceData referenceData = null;
         if (!references.TryGetValue(current.child, out referenceData))
         {
             referenceData             = new MemoryTracker.ReferenceData();
             referenceData.path        = current.path;
             referenceData.pathCost    = 0;
             references[current.child] = referenceData;
         }
         if (!seen.Contains(current.child))
         {
             seen.Add(current.child);
             toProcess.Enqueue(current.child);
         }
     }
 }
Пример #19
0
        private static IEnumerable <ChildReference> DistillChildReferencesFromObject(object current, object parent, string currentPath)
        {
            Type type = current.GetType();

            if (type.IsClass)
            {
                yield return(new ChildReference
                {
                    child = current,
                    path = currentPath
                });

                /*Error: Unable to find new state assignment for yield return*/;
            }
            if (!type.IsPrimitive)
            {
                if (!type.IsValueType)
                {
                    throw new NotImplementedException();
                }
                string structPath = currentPath + ".";
                using (IEnumerator <ChildReference> enumerator = MemoryTracker.GetAllReferencedClassesFromClassOrStruct(current, MemoryTracker.GetFieldsFromHierarchy(type, BindingFlags.Instance), parent, structPath).GetEnumerator())
                {
                    if (enumerator.MoveNext())
                    {
                        ChildReference childReference = enumerator.Current;
                        yield return(childReference);

                        /*Error: Unable to find new state assignment for yield return*/;
                    }
                }
            }
            yield break;
IL_017e:
            /*Error near IL_017f: Unexpected return in MoveNext()*/;
        }
Пример #20
0
 public static void LogObjectHoldPathsFor(IEnumerable <WeakReference> elements, Func <WeakReference, int> weight)
 {
     GC.Collect();
     MemoryTracker.LockTracking();
     try
     {
         Dictionary <object, MemoryTracker.ReferenceData> dictionary = new Dictionary <object, MemoryTracker.ReferenceData>();
         HashSet <object> hashSet = new HashSet <object>();
         MemoryTracker.foundCollections.Clear();
         Queue <object> queue = new Queue <object>();
         foreach (object current in from weakref in MemoryTracker.tracked.SelectMany((KeyValuePair <Type, HashSet <WeakReference> > kvp) => kvp.Value)
                  where weakref.IsAlive
                  select weakref.Target)
         {
             if (!hashSet.Contains(current))
             {
                 hashSet.Add(current);
                 queue.Enqueue(current);
             }
         }
         foreach (Type current2 in GenTypes.AllTypes.Union(MemoryTracker.tracked.Keys))
         {
             if (!current2.FullName.Contains("MemoryTracker") && !current2.FullName.Contains("CollectionsTracker"))
             {
                 if (!current2.ContainsGenericParameters)
                 {
                     MemoryTracker.AccumulateStaticMembers(current2, dictionary, hashSet, queue);
                 }
             }
         }
         int num = 0;
         while (queue.Count > 0)
         {
             if (num % 10000 == 0)
             {
                 UnityEngine.Debug.LogFormat("{0} / {1} (to process: {2})", new object[]
                 {
                     num,
                     num + queue.Count,
                     queue.Count
                 });
             }
             num++;
             MemoryTracker.AccumulateReferences(queue.Dequeue(), dictionary, hashSet, queue);
         }
         if (elements != null && weight != null)
         {
             int num2 = 0;
             MemoryTracker.CalculateReferencePaths(dictionary, from kvp in dictionary
                                                   where !kvp.Value.path.NullOrEmpty()
                                                   select kvp.Key, num2);
             num2 += 1000;
             MemoryTracker.CalculateReferencePaths(dictionary, from kvp in dictionary
                                                   where kvp.Value.path.NullOrEmpty() && kvp.Value.referredBy.Count == 0
                                                   select kvp.Key, num2);
             foreach (object current3 in from kvp in dictionary
                      where kvp.Value.path.NullOrEmpty()
                      select kvp.Key)
             {
                 num2 += 1000;
                 MemoryTracker.CalculateReferencePaths(dictionary, new object[]
                 {
                     current3
                 }, num2);
             }
             Dictionary <string, int> dictionary2 = new Dictionary <string, int>();
             foreach (WeakReference current4 in elements)
             {
                 if (current4.IsAlive)
                 {
                     string path = dictionary[current4.Target].path;
                     if (!dictionary2.ContainsKey(path))
                     {
                         dictionary2[path] = 0;
                     }
                     Dictionary <string, int> dictionary3;
                     string key;
                     (dictionary3 = dictionary2)[key = path] = dictionary3[key] + weight(current4);
                 }
             }
             StringBuilder stringBuilder = new StringBuilder();
             foreach (KeyValuePair <string, int> current5 in from kvp in dictionary2
                      orderby - kvp.Value
                      select kvp)
             {
                 stringBuilder.AppendLine(string.Format("{0}: {1}", current5.Value, current5.Key));
             }
             Log.Message(stringBuilder.ToString());
         }
     }
     finally
     {
         MemoryTracker.UnlockTracking();
     }
 }
Пример #21
0
 internal void <> m__0()
 {
     MemoryTracker.LogObjectHoldPathsFor(this.trackedBatch, (WeakReference _) => 1);
 }