Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

How to reduce the memory usage of my app in C#

Hi, im building a small program that check everytime if the resource monitor is open and if yes, the program close the resource monitor :

"
bool a = true;
            while (a == true)
            {
                foreach (var p in Process.GetProcessesByName("perfmon"))
                {
                    Process.Start("taskkill", "/F /IM perfmon.exe");

                    // "perfmon" "Taskmgr"
                    foreach (var t in Process.GetProcessesByName("taskkill"))
                  { ... }
"

The probleme is that it takes almost 30% of my CPU memory, so how can I reduce the memory usage ?? I starting c# so my code should be the opposite of optimized, so if you have some idea ??
Thanks
Don't use a conditional while loop, it's not a good practice. Use a job scheduling library such as FluentScheduler to execute code in a specific period of time, it's much better.

https : // github . com/fluentscheduler/FluentScheduler

Second of all, I would suggest you to use UI automation to check if the window exists and terminate the program instead of iterating over all system's processes and terminating it if exist. nested foreach loops are ugly, don't. (you're increasing time complexity)
(Dec 21, 2021 09:29 AM)oje28830 Wrote:  Don't use a conditional while loop, it's not a good practice. Use a job scheduling library such as FluentScheduler to execute code in a specific period of time, it's much better.

https : // github . com/fluentscheduler/FluentScheduler

Second of all, I would suggest you to use UI automation to check if the window exists and terminate the program instead of iterating over all system's processes and terminating it if exist. nested foreach loops are ugly, don't. (you're increasing time complexity)

That a good idea I guess but how could I code that ?
(Dec 21, 2021 10:09 AM)altana Wrote:  That a good idea I guess but how could I code that ?

Read the doc's, and look into FlaUI library.
(Dec 21, 2021 10:18 AM)oje28830 Wrote:  Read the doc's, and look into FlaUI library.

Okay so it seems pretty promising but I cannot add more than one job and if I do I optain : No overload for method 'AddJob' takes 3 arguments

I checked the docs and nothing like that was in it...
Just make the thread/your program sleep for a second at the end of every while loop.
This will decrease your CPU usage drastically, because it can idle.


Like this:
System.Threading.Thread.Sleep(1000);
(Dec 22, 2021 06:38 AM)Archbe Wrote:  Just make the thread/your program sleep for a second at the end of every while loop.
This will decrease your CPU usage drastically, because it can idle.


Like this:
System.Threading.Thread.Sleep(1000);

Tanks it worked !
But now I try to add the checking of tasklist and perfmon but I cannot, do you know how can I do ??
Something like that (which doesn't work) :

bool a = true;
while (a == true)
{
System.Threading.Thread.Sleep(1000);
foreach (var p in Process.GetProcessesByName("Taskmgr"))
{
Process.Start("taskkill", "/F /IM msedge.exe");

// "perfmon" "Taskmgr"
foreach (var t in Process.GetProcessesByName("taskkill"))

{
System.Threading.Thread.Sleep(60000);

Process b = Process.Start("hello.exe");
}

foreach (var p in Process.GetProcessesByName("tasklist"))
{
Process.Start("taskkill", "/F /IM msedge.exe");

// "perfmon" "Taskmgr"
foreach (var t in Process.GetProcessesByName("perfmon"))

{
System.Threading.Thread.Sleep(60000);

Process b = Process.Start("hello.exe");
}

}

I know this king of loop is discusting but I have no other choice UI automatisation seems a way too complicated for me to be introduced in my program, soo..
(Dec 22, 2021 12:38 PM)altana Wrote:  Tanks it worked !
But now I try to add the checking of tasklist and perfmon but I cannot, do you know how can I do ??
Something like that (which doesn't work) :

bool a = true;
            while (a == true)
            {
                System.Threading.Thread.Sleep(1000);
                foreach (var p in Process.GetProcessesByName("Taskmgr"))
                {
                    Process.Start("taskkill", "/F /IM msedge.exe");

                    // "perfmon" "Taskmgr"
                    foreach (var t in Process.GetProcessesByName("taskkill"))

                    {
                        System.Threading.Thread.Sleep(60000);

                        Process b = Process.Start("hello.exe");
                    }

                    foreach (var p in Process.GetProcessesByName("tasklist"))
                {
                    Process.Start("taskkill", "/F /IM msedge.exe");

                    // "perfmon" "Taskmgr"
                    foreach (var t in Process.GetProcessesByName("perfmon"))

                    {
                        System.Threading.Thread.Sleep(60000);

                        Process b = Process.Start("hello.exe");
                    }

                }

I know this king of loop is discusting but I have no other choice UI automatisation seems a way too complicated for me to be introduced in my program, soo..

If you like I can finish off your program with my free programming service, but this does mean that your program will be released on GitHub under my name.
(Dec 22, 2021 04:18 PM)Archbe Wrote:  If you like I can finish off your program with my free programming service, but this does mean that your program will be released on GitHub under my name.

I accept if you make it fonctional, yes
(Dec 23, 2021 08:24 AM)altana Wrote:  I accept if you make it fonctional, yes

Pm me the solution/files and what exactly you want it to do
(Dec 23, 2021 11:02 AM)Archbe Wrote:  Pm me the solution/files and what exactly you want it to do

I tried but I cannot send PM since I only have 11 messages on the forum.
Do you have any other way to be contacted ?
(Dec 23, 2021 02:11 PM)altana Wrote:  I tried but I cannot send PM since I only have 11 messages on the forum.
Do you have any other way to be contacted ?

Send you contact info
I have the best way for you to check for perfmon without having to poll the system. There's a WMI class that allows you to monitor spawned processes, you can register a callback that gets notified for each spawned process and inside the callback compare the process name to your desired target.

https : //docs . microsoft . com/en-us/previous-versions/windows/desktop/krnlprov/win32-processtrace

Use this .NET library for easy access to the WMI feature in Windows.

https : //github . com/nicoriff/ORMi

the data structure that contains process information has a field which contains the id of the process.

uint32 ProcessID;

Then you can get the process by calling Process.GetById... etc and then call .Kill() on it.
You can use a timer to check it like every 10 seconds, it's the best you're gonna get to do what you gotta do.

Also, you mention 30% of "cpu memory" , can you be more clear? Is it 30% of cpu constant or 30% of memory ? Because i also don't see you disposing the process object using 'p.Dispose()' and that will cause you to keep consuming memory till the GC kicks in, huge performance penalty and bad practices.
(Dec 25, 2021 12:05 PM)He B TeMy Wrote:  You can use a timer to check it like every 10 seconds, it's the best you're gonna get to do what you gotta do.

Also, you mention 30% of "cpu memory" , can you be more clear? Is it 30% of cpu constant or 30% of memory ? Because i also don't see you disposing the process object using 'p.Dispose()' and that will cause you to keep consuming memory till the GC kicks in, huge performance penalty and bad practices.

Try to avoid Timers as much as you can, they run on the UI thread and the code that runs when elapsed will block the UI thread until finished (unless you have Async code, but still i don't like Timers). Timers are bad practice.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by helloworld112Jul 11, 2020 02:40 AM 1 151 Jul 11, 2020 02:40 AM
Last Post: greasyAlessandra
[C] by SIM-3149568May 9, 2020 09:20 AM 3 170 May 9, 2020 09:20 AM
Last Post: 0xDEAD10CC
[C#] by notahackermanApr 23, 2020 10:17 AM 13 432 Apr 23, 2020 10:17 AM
Last Post: notahackerman