Exemple #1
0
        private SortableBindingList <ManagedObject> CompareSnapshots(Snapshot snapshot1, Snapshot snapshot2, string filter, Boolean?onlyChangedFilter)
        {
            if (snapshot1 == null || snapshot2 == null)
            {
                return(null);
            }

            SortableBindingList <ManagedObject> managedObjectsCompare = new SortableBindingList <ManagedObject>();

            foreach (ManagedObject mo in snapshot1.ManagedObjectDic.Values)
            {
                if (filter.Length != 0 && !mo.ObjectName.Contains(filter))
                {
                    continue;
                }

                if (snapshot2.ManagedObjectDic.ContainsKey(mo.ObjectName))
                {
                    mo.ObjectCountLastHelper = snapshot2.ManagedObjectDic[mo.ObjectName].ObjectCount;
                }

                if ((!onlyChangedFilter.HasValue || !onlyChangedFilter.Value || mo.ObjectChange > 0) &&
                    (typeFilter.Count == 0 || typeFilter.Contains(mo.ObjectName)))
                {
                    managedObjectsCompare.Add(mo);
                }
            }

            if (!onlyChangedFilter.HasValue || !onlyChangedFilter.Value)
            {
                foreach (ManagedObject mo in snapshot2.ManagedObjectDic.Values)
                {
                    if (filter.Length != 0 && !mo.ObjectName.Contains(filter))
                    {
                        continue;
                    }

                    if (!snapshot1.ManagedObjectDic.ContainsKey(mo.ObjectName) &&
                        (typeFilter.Count == 0 || typeFilter.Contains(mo.ObjectName)))
                    {
                        ManagedObject fakeObjekt = new ManagedObject()
                        {
                            ObjectCountLastHelper = mo.ObjectCount,
                            ObjectName            = mo.ObjectName,
                            ObjectSize            = mo.ObjectSize,
                            ObjectCount           = 0
                        };

                        managedObjectsCompare.Add(fakeObjekt);
                    }
                }
            }

            return(managedObjectsCompare);
        }
Exemple #2
0
 private void copyObjectNameToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (dataGridViewMain.SelectedRows.Count > 0)
     {
         ManagedObject m = dataGridViewMain.SelectedRows[0].DataBoundItem as ManagedObject;
         if (m != null)
         {
             Clipboard.SetText(m.ObjectName);
         }
     }
 }
Exemple #3
0
 private void filterForSelectedTypesToolStripMenuItem_Click(object sender, EventArgs e)
 {
     foreach (DataGridViewRow row in dataGridViewMain.SelectedRows)
     {
         ManagedObject m = row.DataBoundItem as ManagedObject;
         if (m != null)
         {
             typeFilter.Add(m.ObjectName);
         }
     }
     CompareSnapshotsAndDisplay(snapshot1Current, snapshot2Current);
 }
Exemple #4
0
        private static SortedDictionary <string, ManagedObject> ListObjects(ClrRuntime runtime)
        {
            SortedDictionary <String, ManagedObject> managedObjects = new SortedDictionary <string, ManagedObject>();

            if (runtime.Heap.CanWalkHeap)
            {
                foreach (ulong ptr in runtime.Heap.EnumerateObjectAddresses())
                {
                    ClrType type = runtime.Heap.GetObjectType(ptr);

                    //Free objects are not real objects in the strictest sense. They are actually markers placed by the GC to
                    //denote free space on the heap. Free objects have no fields (though they do have a size). In general, if
                    //you are trying to find heap fragmentation, you will need to take a look at how many Free objects there are,
                    //how big they are, and what lies between them. Otherwise, you should ignore them.
                    //https://github.com/Microsoft/clrmd/blob/master/Documentation/TypesAndFields.md
                    if (type == null || type.IsFree)
                    {
                        continue;
                    }

                    ManagedObject mo = null;
                    if (!managedObjects.ContainsKey(type.Name))
                    {
                        mo = new ManagedObject()
                        {
                            ObjectName = type.Name
                        };
                        managedObjects.Add(type.Name, mo);
                    }

                    if (mo == null)
                    {
                        mo = managedObjects[type.Name];
                    }

                    mo.ObjectCount++;
                    mo.ObjectSize += type.GetSize(ptr);
                }
            }
            return(managedObjects);
        }
Exemple #5
0
        private void buttonReport_Click(object sender, EventArgs e)
        {
            if (Snapshots.Count == 0)
            {
                return;
            }

            saveFileDialogReport.FileName = String.Format("clrmd_{0:yyyyMMdd_HHmmss}", Snapshots.Count > 0 ? Snapshots[0].Date : DateTime.Now);
            if (saveFileDialogReport.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            Cursor.Current = Cursors.WaitCursor;
            SortedList <String, List <ManagedObject> > fullObjectList = new SortedList <string, List <ManagedObject> >();
            Snapshot lastSnapshot = null;

            foreach (Snapshot s in Snapshots)
            {
                SortableBindingList <ManagedObject> comparedSnapshots = CompareSnapshots(s, lastSnapshot, String.Empty, null);
                foreach (string n in s.ManagedObjectDic.Keys)
                {
                    if (!fullObjectList.ContainsKey(n))
                    {
                        fullObjectList.Add(n, new List <ManagedObject>());
                    }

                    if (comparedSnapshots != null)
                    {
                        ManagedObject mo = comparedSnapshots.FirstOrDefault(x => x.ObjectName == n);

                        if (mo != null)
                        {
                            mo.SnapshotKey = s.Date;
                            fullObjectList[n].Add(mo);
                        }
                    }
                    else
                    {
                        if (Snapshots[0].ManagedObjectDic.ContainsKey(n))
                        {
                            Snapshots[0].ManagedObjectDic[n].SnapshotKey = s.Date;
                            fullObjectList[n].Add(Snapshots[0].ManagedObjectDic[n]);
                        }
                    }
                }

                lastSnapshot = s;
            }

            StringBuilder sb = new StringBuilder();

            sb.Append("ObjectName;Sum;");
            sb.AppendLine(String.Join(";", Snapshots.Select(x => x.Date.ToString("HH:mm:ss"))));

            foreach (KeyValuePair <String, List <ManagedObject> > smo in fullObjectList.OrderBy(x => x.Key))
            {
                sb.Append(smo.Key + ";");
                sb.Append(smo.Value.Sum(x => x.ObjectChange));
                sb.Append(";");
                foreach (Snapshot s in Snapshots)
                {
                    ManagedObject mo = smo.Value.FirstOrDefault(x => x.SnapshotKey == s.Date);
                    if (mo != null && mo.ObjectChange != 0)
                    {
                        sb.Append(mo.ObjectChange);
                    }
                    else
                    {
                        sb.Append(0);
                    }
                    sb.Append(";");
                }
                sb.AppendLine();
            }
            Cursor.Current = Cursors.Default;

            File.WriteAllText(saveFileDialogReport.FileName, sb.ToString());
        }