-1

We have C#.Net windows application, from this application i need to track the memory and CPU usage details of the currently running process. I tried the below mentioned code to get memory details.

Process curProcess=Process.GetCurrentProcess();

Console.Writeline(curProcess.PrivateMemorySize64);

Console.Writeline(curProcess.VirtualMemorySize64);

Console.Writeline(curProcess.PagedMemorySize64);

Console.Writeline(curProcess.NonpagedSystemMemorySize64);

var counter = new PerformanceCounter("Process", "Working Set - Private", Process.GetCurrentProcess().ProcessName);

Console.Writeline(curProcess.PagedMemorySize64);

Console.Writeline(counter.RawValue);

All the above code give the whole process memory details, But my requirement is to get the below details,

  • List of objects in memory and corresponding object's memory allocations,
  • Amount of memory collected by Garbage collector,
  • Un-Disposed objects count and its name,
  • Process threads and its relationships.

    Please send me some code samples to achieve my requirement.

NOTE: I tried CLR profile and ANTS profile to get the detailed information about the memory, but i really need the sample code to achieve inside my application.

Thank you very much.

H H
  • 263,252
  • 30
  • 330
  • 514
  • I don't know of any profiler that does its thing in-process like that. But perhaps you can find something useful in http://stackoverflow.com/q/3927/56778 – Jim Mischel Jul 25 '13 at 15:39
  • ya, ANTS profile showing the detailed information about each object memory allocation, i really need the sample code to do the same. – sivaprakash Jul 25 '13 at 15:44

1 Answers1

0

For getting information about processor, Operating System, etc:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;

namespace HaardDiskInfoCSharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ManagementObjectSearcher mosProcessor = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");

            foreach (ManagementObject moProcessor in mosProcessor.Get())
            {

                if(moProcessor["maxclockspeed"]!=null)
                    lblPMCSpeed.Text = moProcessor["maxclockspeed"].ToString();
                if(moProcessor["datawidth"]!=null)
                    lblPDataWidth.Text = moProcessor["datawidth"].ToString();
                if(moProcessor["name"]!=null)
                    lblPName.Text=moProcessor["name"].ToString();
                if(moProcessor["manufacturer"]!=null)
                    lblPManufacture.Text = moProcessor["manufacturer"].ToString();

            }
        }
    }
}

Source.

DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105