Killing processes with no CPU usage in a specific time

Mohammad Ganji 1 Reputation point
2021-03-18T16:07:56.203+00:00

Hi,
There is a bug in an application that should be remediated but for now, this is what I'm going to do:

Problem:
Instances (processes) with the same name are produced (for example, worker.exe)
The users create these instances to do their job and then leave the app but the instance is not killed by the app and remains running, doesn't free up the memory while there is no CPU usage. Therefore, you can see many worker.exe processes with 0% CPU usage and hundreds of megabytes of RAM used by each of them which leads to high memory utilization on the server.

What I need:
I'd like to write a script to monitor these processes and kill them if they're not using CPU and have been present for, let's say 15 minutes, to make a workaround temporarily. I hope I've made myself clear here. BTW, O.S. is Windows server 2016 and 2019.

Regards,
M. Ganji

Windows for business Windows Server User experience PowerShell
{count} votes

6 answers

Sort by: Most helpful
  1. Mohammad Ganji 1 Reputation point
    2021-03-18T18:49:01.163+00:00

    Hi,
    It won't.
    The process starts using CPU and when done the RAM is not freed and the process uses about 500GB of RAM but the CPU remains 0%. So, any process which is using 0% CPU for 15 minutes can be killed. If it even uses 1% CPU the 15 minutes timer should be restarted but I'm nearly sure that when it goes to 0% it will remain 0% for eternity.

    Regards,

    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2021-03-18T19:20:22.863+00:00

    This isn't as easy as you might think.

    Each process has a "Responding" property, but all processes that have a "MainWindowHandle" (that isn't a zero) have a window and all processes that have a window always report that they're "Responding". So unless your application is a console application that's useless.

    If you're looking for "zombie" or ("orphaned") processes (ones that have a ParentProcessID) but the parent process has ended, then that's usually pretty easy: for each process, the ParentProcessID should exist as a process. You can't use Get-Process for this, but Get-WMIObject or Get-CIMInstance will report the ParentProcessID (using the Win32_Process class).

    But if you're looking for "idle" processes you'd have to keep track of each process (or maybe just ones with "ProcessName" that's the same as you application -- minus the file extension). You'd also have to separate the "user" time and "system" time. If both are non-varying between checks then they might be candidates to be killed. You may also be able to check the owner of each process and if the owner is no longer present on the server the process may be a candidate for termination. You'd have to update the information and add new processes and remove processes that have ended normally.

    0 comments No comments

  3. Mohammad Ganji 1 Reputation point
    2021-03-19T06:05:53.973+00:00

    Thanks, Rich,
    I think I've already elaborated on what I want. If that is not easy to get, well, that's another thing.

    To make it easier, I'd like to check processes for a 15-minute period with 1s intervals. If they never get above 1% CPU usage, they're candidates to be killed. Is this possible?

    Or let's break it into small steps. How can I get live CPU usage of a process (as observed in performance monitor of task manager in Windows) via Powershell?

    Thanks

    0 comments No comments

  4. Anonymous
    2021-03-19T13:37:07.397+00:00

    Hi,

    You may try the counter "\Process(chrome)\% Processor Time" although it's somewhat different.

    Get-Counter -Counter "\Process(chrome)\% Processor Time" | Select-Object -ExpandProperty CounterSamples | Select-Object -ExpandProperty CookedValue  
    

    For more details you may refer to this link
    https://social.technet.microsoft.com/wiki/contents/articles/12984.understanding-processor-processor-time-and-process-processor-time.aspx

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  5. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-03-19T18:37:16.063+00:00

    Hi @Mohammad Ganji ,

    maybe this is helpful?

    https://gist.github.com/Badgerati/fcc89d73b9546f5c675adaee209581a3

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.