Using LINQ to Find Top 5 Processes that are Consuming Memory

A user on a forum recently asked me how to find the processes that were currently running. A quick look at MSDN led me to the Process object. The Process provides access to local and remote processes and enables you to start and stop local system processes. Here’s how to find out the top five processes that are consuming memory.

C#

var query = (from p in System.Diagnostics.Process.GetProcesses()             orderby p.PrivateMemorySize64 descending             select p)                 .Skip(0)                 .Take(5)                 .ToList();foreach (var item in query){    System.Diagnostics.Debug.WriteLine(item.ProcessName);}

VB.NET

Dim query = ( _        From p In System.Diagnostics.Process.GetProcesses() _        Order By p.PrivateMemorySize64 Descending _        Select p).Skip(0).Take(5).ToList()For Each item In query    System.Diagnostics.Debug.WriteLine(item.ProcessName)Next item

The code above produces the following results on my computer:

clip_image002

Share:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • DZone
  • LinkedIn
  • Reddit
  • StumbleUpon
  • Twitthis
This entry was posted in LINQ, Syndicated. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam Protection by WP-SpamFree