コード例 #1
0
ファイル: Class1.cs プロジェクト: digeler/clrmdhelper
        public void Memenum()
        {
            Console.WriteLine("****Dumping Mem****");

            foreach (var region in (from r in runtime.EnumerateMemoryRegions()
                                    where r.Type != ClrMemoryRegionType.ReservedGCSegment
                                    group r by r.Type into g
                                    let total = g.Sum(p => (uint)p.Size)
                                                orderby total descending
                                                select new
            {
                TotalSize = total,
                Count = g.Count(),
                Type = g.Key
            }))
            {
                Console.WriteLine("{0,6:n0} {1,12:n0} {2}", region.Count, region.TotalSize, region.Type);
            }
            Console.WriteLine("Loader Heap: contains CLR structures and the type system");
            Console.WriteLine("High Frequency Heap: statics, MethodTables, FieldDescs, interface map");
            Console.WriteLine("Low Frequency Heap: EEClass, ClassLoader and lookup tables");
            Console.WriteLine("Stub Heap: stubs for CAS, COM wrappers, PInvoke");
            Console.WriteLine("Large Object Heap: memory allocations that require more than 85k bytes");
            Console.WriteLine("GC Heap: user allocated heap memory private to the app");
            Console.WriteLine("JIT Code Heap: memory allocated by mscoreee (Execution Engine) and the JIT compiler for managed code");
            Console.WriteLine("Process/Base Heap: interop/unmanaged allocations, native memory, etc");
            Console.WriteLine("In general, only the GC Segments (and Reserve GC Segments) should really be eating much memory in your process. The total memory for each other type of heap in your process should be less than 50 megs (slightly more for very large dumps). If something is eating more than 100 megs of memory (and it's not a GC Segment) then that's a red flag for investigation.");
        }
コード例 #2
0
        private static void PrintMemoryRegionInfo(ClrRuntime runtime)
        {
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine("\nMemory Region Information");
            Console.ResetColor();
            var seperator = "------------------------------------------------";

            Console.WriteLine(seperator);
            Console.WriteLine("{0,24} {1,4} {2,15}", "Type", "Count", "Total Size (MB)");
            Console.WriteLine(seperator);
            foreach (var region in (from r in runtime.EnumerateMemoryRegions()
                                    //where r.Type != ClrMemoryRegionType.ReservedGCSegment
                                    group r by r.Type into g
                                    let total = g.Sum(p => (uint)p.Size)
                                                orderby total descending
                                                select new
            {
                TotalSize = total,
                Count = g.Count(),
                Type = g.Key
            }))
            {
                Console.WriteLine("{0,24} {1,5} {2,15:N2}", region.Type.ToString(), region.Count, region.TotalSize / 1024.0 / 1024.0);
            }
            Console.WriteLine(seperator);
        }
コード例 #3
0
ファイル: MainForm.cs プロジェクト: czapek/MemoryDiagnostics
        private static void CollectMemory(ClrRuntime runtime, Snapshot s)
        {
            foreach (ClrMemoryRegion r in runtime.EnumerateMemoryRegions())
            {
                if (r.Type == ClrMemoryRegionType.GCSegment)
                {
                    if (r.GCSegmentType == GCSegmentType.Ephemeral)
                    {
                        s.MemoryEphemeral += r.Size;
                    }

                    if (r.GCSegmentType == GCSegmentType.LargeObject)
                    {
                        s.MemoryLargeObject += r.Size;
                    }

                    if (r.GCSegmentType == GCSegmentType.Regular)
                    {
                        s.MemoryRegular += r.Size;
                    }
                }
                else if (r.Type == ClrMemoryRegionType.ReservedGCSegment)
                {
                    s.MemoryReserved += r.Size;
                }
                else
                {
                    s.MemoryOther += r.Size;
                }
            }
        }
コード例 #4
0
ファイル: ClrmdReader.cs プロジェクト: guitarrapc/dotnet-lab
 // OUTPUT SAMPLE
 // ------------------
 //  2       12.915MB GCSegment
 //130        9.462MB LowFrequencyLoaderHeap
 //102        6.627MB HighFrequencyLoaderHeap
 //  2        0.713MB ResolveHeap
 //  2        0.401MB DispatchHeap
 //  2        0.074MB CacheEntryHeap
 //  2        0.049MB IndcellHeap
 //  2        0.033MB LookupHeap
 //  2        0.025MB StubHeap
 //  1        0.012MB HandleTableChunk
 // ------------------
 public void ShowMemoryRegion(ClrRuntime runtime)
 {
     foreach (var region in (from r in runtime.EnumerateMemoryRegions()
                             where r.Type != ClrMemoryRegionType.ReservedGCSegment
                             group r by r.Type into g
                             let total = g.Sum(p => (uint)p.Size)
                                         orderby total descending
                                         select new
     {
         TotalSize = total,
         Count = g.Count(),
         Type = g.Key
     }))
     {
         Console.WriteLine("{0,6:n0} {1,12:n3}MB {2}", region.Count, region.TotalSize / 1000d / 1000d, region.Type);
     }
     Console.WriteLine();
 }
コード例 #5
0
        private static void PrintSegments(ClrRuntime runtime, StreamWriter sw)
        {
            var regions = (from r in runtime.EnumerateMemoryRegions()
                           where r.Type != ClrMemoryRegionType.ReservedGCSegment
                           group r by r.Type into g
                           let total = g.Sum(p => (uint)p.Size)
                                       orderby total descending
                                       select new
            {
                TotalSize = total,
                Count = g.Count(),
                Type = g.Key
            });

            foreach (var region in regions)
            {
                sw.WriteLine("{0,6:n0} {1,12:n0} {2}", region.Count, region.TotalSize, region.Type);
            }
        }
コード例 #6
0
        private void AddCLRRegions(ClrRuntime runtime)
        {
            foreach (var region in runtime.EnumerateMemoryRegions())
            {
                // We don't need reserved memory in our dump
                if (region.Type == ClrMemoryRegionType.ReservedGCSegment)
                {
                    continue;
                }

                ulong address    = region.Address;
                ulong endAddress = region.Address + region.Size;
                ulong existingEndAddress;
                if (_majorClrRegions.Find(ref address, out existingEndAddress))
                {
                    _majorClrRegions.Update(region.Address, Math.Max(existingEndAddress, endAddress));
                }
                else
                {
                    _majorClrRegions.Add(region.Address, endAddress);
                }
            }
        }
コード例 #7
0
        private static IEnumerable <MemoryRegionInfo> GetMemoryRegions(ClrRuntime runtime)
        {
            Log.Verbose("Getting Memory Regions");

            var memoryRegionInfos = runtime.EnumerateMemoryRegions()
                                    .Where(r => r.Type != ClrMemoryRegionType.ReservedGCSegment)
                                    .GroupBy(r => r.Type)
                                    .Select(group => new
            {
                g     = group,
                total = group.Sum(p => (uint)p.Size)
            })
                                    .OrderByDescending(t => t.total)
                                    .Select(t => new MemoryRegionInfo
            {
                TotalSize = t.total,
                Count     = t.g.Count(),
                Type      = t.g.Key
            })
                                    .ToList();

            return(memoryRegionInfos);
        }
コード例 #8
0
        static void Main(string[] args)
        {
            string dump, dac;

            if (!TryParseArgs(args, out dump, out dac))
            {
                Usage();
                Environment.Exit(1);
            }

            try
            {
                // Create a ClrRuntime instance from the dump and dac location.  The ClrRuntime
                // object represents a version of CLR loaded in the process.  It contains data
                // such as the managed threads in the process, the AppDomains in the process,
                // the managed heap, and so on.
                ClrRuntime runtime = CreateRuntime(dump, dac);

                // To get memory statistics, you can use EnumerateMemoryRegions.  This enumerates
                // the address and size of every memory region (I.E., heap) that CLR allocates.
                // You can use this information to track down what's "too large" in your process.
                Dictionary <ClrMemoryRegionType, Entry> stats = new Dictionary <ClrMemoryRegionType, Entry>();
                foreach (var region in runtime.EnumerateMemoryRegions())
                {
                    Entry entry;
                    if (!stats.TryGetValue(region.Type, out entry))
                    {
                        entry = new Entry();
                        stats[region.Type] = entry;
                    }

                    entry.Regions.Add(region);
                    entry.Size += region.Size;
                }

                // Print out total stats
                var sortedEntries = from t in stats.Values
                                    orderby t.Size
                                    select t;

                Console.WriteLine("Total stats for {0} AppDomain{1}:", runtime.AppDomains.Count, runtime.AppDomains.Count > 1 ? "s" : "");
                Console.WriteLine("{0,12} {1}", "Size", "Memory Type");

                foreach (var entry in sortedEntries)
                {
                    Console.WriteLine("{0,12:n0} {1}", entry.Size, entry.Name);
                }

                // Print out per-appdomain usage.  You could probably get more clever with linq here,
                // but I tried to keep this as simple as possible.
                foreach (ClrAppDomain ad in runtime.AppDomains)
                {
                    Console.WriteLine();
                    Console.WriteLine("Memory usage for AppDomain '{0}':", ad.Name);
                    foreach (Entry entry in stats.Values)
                    {
                        if (!entry.HasAppDomainData)
                        {
                            continue;
                        }

                        long size = entry.Regions.Where(p => p.AppDomain == ad).Sum(p => (uint)p.Size);
                        if (size > 0)
                        {
                            Console.WriteLine("{0,12:n0} {1}", size, entry.Name);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unhandled exception:");
                Console.WriteLine(ex);
            }
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: vijen12000/pro-.net-memory
        static void Main(string[] args)
        {
            int pid = 25668; //Process.GetCurrentProcess().Id;

            // Listing 15-26
            // DataTarget target = DataTarget.LoadCrashDump(@"c:\work\crash.dmp")
            using (DataTarget target = DataTarget.AttachToProcess(pid, 5000, AttachFlag.Invasive))
            {
                foreach (ClrInfo clrInfo in target.ClrVersions)
                {
                    Console.WriteLine("Found CLR Version:" + clrInfo.Version.ToString());

                    // This is the data needed to request the dac from the symbol server:
                    ModuleInfo dacInfo = clrInfo.DacInfo;
                    Console.WriteLine($"Filesize:  {dacInfo.FileSize:X}");
                    Console.WriteLine($"Timestamp: {dacInfo.TimeStamp:X}");
                    Console.WriteLine($"Dac File:  {dacInfo.FileName}");

                    ClrRuntime runtime = clrInfo.CreateRuntime();

                    // Listing 15-27
                    foreach (ClrThread thread in runtime.Threads)
                    {
                        if (!thread.IsAlive)
                        {
                            continue;
                        }
                        Console.WriteLine("Thread {0:X}:", thread.OSThreadId);
                        foreach (ClrStackFrame frame in thread.StackTrace)
                        {
                            Console.WriteLine("{0,12:X} {1,12:X} {2}", frame.StackPointer, frame.InstructionPointer,
                                              frame.ToString());
                        }
                        Console.WriteLine();
                    }

                    // Listing 15-28
                    foreach (var domain in runtime.AppDomains)
                    {
                        Console.WriteLine($"AppDomain {domain.Name} ({domain.Address:X})");
                        foreach (var module in domain.Modules)
                        {
                            Console.WriteLine($"   Module {module.Name} ({(module.IsFile ? module.FileName : "")})");
                            foreach (var type in module.EnumerateTypes())
                            {
                                Console.WriteLine($"{type.Name} Fields: {type.Fields.Count}");
                            }
                        }
                    }

                    // Listing 15-29
                    foreach (var region in runtime.EnumerateMemoryRegions().OrderBy(r => r.Address))
                    {
                        Console.WriteLine($"{region.Address:X} (size: {region.Size:N0}) - {region.Type} " +
                                          $"{(region.Type == ClrMemoryRegionType.GCSegment ? "(" + region.GCSegmentType.ToString() + ")" : "")}");
                    }

                    // Listing 15-30
                    ClrHeap heap = runtime.Heap;
                    foreach (var clrObject in heap.EnumerateObjects())
                    {
                        if (clrObject.Type.Name.EndsWith("SampleClass"))
                        {
                            ShowObject(heap, clrObject, string.Empty);
                        }
                    }

                    // Listing 15-33
                    foreach (var segment in heap.Segments)
                    {
                        Console.WriteLine(
                            $"{segment.Start:X16} - {segment.End:X16} ({segment.CommittedEnd:X16}) CPU#: {segment.ProcessorAffinity}");
                        if (segment.IsEphemeral)
                        {
                            Console.WriteLine($"   Gen0: {segment.Gen0Start:X16} ({segment.Gen0Length})");
                            Console.WriteLine($"   Gen1: {segment.Gen1Start:X16} ({segment.Gen1Length})");
                            if (segment.Gen2Start >= segment.Start &&
                                segment.Gen2Start < segment.CommittedEnd)
                            {
                                Console.WriteLine($"   Gen2: {segment.Gen2Start:X16} ({segment.Gen2Length})");
                            }
                        }
                        else if (segment.IsLarge)
                        {
                            Console.WriteLine($"   LOH: {segment.Start} ({segment.Length})");
                        }
                        else
                        {
                            Console.WriteLine($"   Gen2: {segment.Gen2Start:X16} ({segment.Gen2Length})");
                        }

                        foreach (var address in segment.EnumerateObjectAddresses())
                        {
                            var type = heap.GetObjectType(address);
                            if (type == heap.Free)
                            {
                                Console.WriteLine($"{type.GetSize(address)}");
                            }
                        }
                    }

                    foreach (ClrThread thread in runtime.Threads)
                    {
                        var mi = runtime.GetType()
                                 .GetMethod("GetThread", BindingFlags.Instance | BindingFlags.NonPublic);
                        var threadData = mi.Invoke(runtime, new object[] { thread.Address });
                        var pi         = threadData.GetType()
                                         .GetProperty("AllocPtr", BindingFlags.Instance | BindingFlags.Public);
                        ulong allocPtr = (ulong)pi.GetValue(threadData);
                    }
                }
            }
        }
コード例 #10
0
 public IEnumerable <ClrMemoryRegion> EEHeap()
 {
     return(_runtime.EnumerateMemoryRegions());
 }
コード例 #11
0
 public void EnumerateMemoryRegions(out IMDMemoryRegionEnum ppEnum)
 {
     ppEnum = new MDMemoryRegionEnum(new List <ClrMemoryRegion>(m_runtime.EnumerateMemoryRegions()));
 }
コード例 #12
0
 /// <summary>
 ///     Enumerates regions of memory which CLR has allocated with a description of what data
 ///     resides at that location.  Note that this does not return every chunk of address space
 ///     that CLR allocates.
 /// </summary>
 /// <returns>An enumeration of memory regions in the process.</returns>
 /// <inheritdoc />
 public IEnumerable <IClrMemoryRegion> EnumerateMemoryRegions() =>
 Runtime.EnumerateMemoryRegions().Select(Converter.Convert);