5

My code extract all names of loaded modules from each running process, my approach goes like this answer.

Here is my code:

Process[] procs = Process.GetProcesses();            
foreach (Process p in procs)
{                  
    foreach (ProcessModule item in p.Modules)
    {
        Console.WriteLine(item.FileName);
    }
}

Fore some reason, this approach have a very low performance :(

Is there another method or a different approach to get all those modules' names?

Any other solution that will run faster than that would be great

TIA

Community
  • 1
  • 1
Eli
  • 4,576
  • 1
  • 27
  • 39
  • 3
    How low is 'very low performance'? – Philip Pittle Aug 12 '14 at 10:39
  • on my System this code is acceptable fast – S.L. Aug 12 '14 at 10:43
  • @PhilipPittle 731 milliseconds for **only** 2097 modules in 39 process – Eli Aug 12 '14 at 10:46
  • Is `tasklist /M` much quicker? – rene Aug 12 '14 at 10:48
  • @rene what is the implementation? – Eli Aug 12 '14 at 10:50
  • I have no idea but I assume the guys in Redmond know how to iterate efficient over the module list...if your implementation meets the performance of commandline you have to assume that the native win32 api doesn't deliver more speed. – rene Aug 12 '14 at 10:53
  • I get 730 ms, but needed some try / catch around the p.Modules loop (otherwise I get an access denied error due to UAC). But that doesn't seem too slow... What happens if you take out the Console.WriteLine, that can sometimes be slow. Also, how many processes / modules are you dealing with here? – David_001 Aug 12 '14 at 11:09
  • @David_001 I get 731 milliseconds for 2097 modules in 39 process – Eli Aug 12 '14 at 11:23

1 Answers1

3

You could try adding some parallelization to speed things up:

 Parallel.ForEach(Process.GetProcesses(),
            process =>
            {
                foreach (ProcessModule m in process.Modules)
                {
                    Console.WriteLine(m.FileName);
                }
            });
Philip Pittle
  • 11,821
  • 8
  • 59
  • 123
  • 2
    @Eli Are you sure it's not making a difference? I tried it and [this is my result](http://i.imgur.com/Fu65m8b.png) – Measurity Aug 12 '14 at 11:29
  • 2
    @Eli - Are you on a machine with multiple cpu cores? – Philip Pittle Aug 12 '14 at 11:40
  • @Measuring right now its not seem to be stable at all. I'll try to make an average on 50 as you did... – Eli Aug 12 '14 at 11:45
  • 1
    @PhilipPittle No. good commant - seem to be the reason that parallel doesn't improve the performance. Anyway I am working on a virtual box, I will test it with more cores - but, still its a very slow mechanism – Eli Aug 12 '14 at 11:56
  • 1
    @Eli - Yes, it is a rather slow process to gather the Modules from all processes. There is no way to get this information other than asking the Operating System for it. And the OS doesn't 'cache' this information, so it has to go and fetch it from every running process. There isn't much benefit from an OS stand point to cache this, so it doesn't warrant the memory overhead. This is why, as `rene` pointed out, even `tasklist /M` is relatively slow as well. – Philip Pittle Aug 12 '14 at 12:05
  • @PhilipPittle OK, if that so I'll accept your answer - I tried it 50 times and the average is better for the parallel approach (despite the fact that I'm currently running with a single core) Many Thanks :) – Eli Aug 12 '14 at 12:15