- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace Test
- {
- static class LanguageFetures
- {
- class ProcessData
- {
- public Int32 ID { get; set; }
- public Int64 Memory { get; set; }
- public string Name { get; set; }
- }
- static void DisplayProcesses( Func<Process, Boolean> match )
- {
- var processes = new List<ProcessData>();
- foreach (var process in Process.GetProcesses())
- {
- if (match(process))
- processes.Add(new ProcessData { ID = process.Id, Name = process.ProcessName, Memory = process.WorkingSet64 });
- }
- Console.WriteLine("Total Memory:{0}MB",processes.TotalMemory()/1024/1024);
- var top2Memory = processes.OrderByDescending(process => process.Memory).Take(2).Sum(process=>process.Memory/1024/1024);
- Console.WriteLine("Memory consumed the two most hungry processes:{0}M",top2Memory);
- }
- static Int64 TotalMemory( this IEnumerable<ProcessData> processes )
- {
- Int64 result = 0;
- foreach (var process in processes)
- {
- result += process.Memory;
- }
- return result;
- }
- static void Main( string[] args )
- {
- DisplayProcesses(process => process.WorkingSet64 >= 20 * 1024 * 1024);
- Console.Read();
- }
- }
- }