Hello guys -

Need help with a windows batch script that would kill processes by the memory usage but leave the last one. For example if i have 10 java.exe processes running i want to kill the top 9 by memory usage and leave the last one alone.

Help appreciated!

-sri

Dani AI

Generated

asked for killing multiple instances by memory while leaving one running; and rightly pointed out built-in tools and filters, but a cleaner, more reliable way is to use PowerShell so you work with process objects (no fragile text parsing). PowerShell lets you preview candidates, sort by exact memory fields, and then stop only the ones you choose — and it runs on modern Windows (or can be added on older systems).

Preview the processes you would kill (WorkingSet64 is in bytes), then run the termination only after verifying the list:

Get-Process java |
  Sort-Object -Property WorkingSet64 -Descending |
  Select-Object -First 9 Id,ProcessName,WorkingSet64

Once the preview looks correct, terminate those nine largest java processes:

Get-Process java |
  Sort-Object -Property WorkingSet64 -Descending |
  Select-Object -First 9 |
  ForEach-Object { Stop-Process -Id $_.Id -Force }

Notes and cautions: run PowerShell elevated if some processes require admin rights; always preview before killing; Stop-Process is immediate and can break running services or data, so ensure the JVMs you kill aren’t providing critical services. To keep the single largest instead of the smallest, invert the selection (skip the first one and stop the rest) or sort ascending depending on which instance you want to preserve. If PowerShell isn’t available, WMIC can list per-process memory (for inspection), but scripting is less convenient. For PowerShell reference see Get-Process documentation.

Recommended Answers

All 2 Replies

Hey there Sri.

For ending processes, you can check out the command 'taskkill' (http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/taskkill.mspx?mfr=true) Your version of windows may or may not have a copy of taskkill.exe in C:\WINDOWS\system32\ ,which is the actual command itself. If not, don't fear! There's tons of copeies of batch commands just like this one online; although you might want to try to find a safe link, download it, and run it thru a virus scan; since it's an exe, you really don't know whats in it. After you found a clean version, drag it into C:\WINDOWS\system32\ and you should be able to use it! As for finding out which process is taking up the most CPU power, batch can't really do that. You can prolly use c or c++ to get access to hardware level stuff like that, and then call a system command in the script for killing a process. In c++ I think it's:

system("you system command here");

hope that helps!

Hello guys -

Need help with a windows batch script that would kill processes by the memory usage but leave the last one. For example if i have 10 java.exe processes running i want to kill the top 9 by memory usage and leave the last one alone.

Help appreciated!

-sri

That's easy!

taskkill -f -fi "PID eq xxx" -fi "MEMUSAGE gt xxxx"

The XXX in "PID" it's the Process PID (you can see it by typing TASKLIST in the cmd)
The XXX in "MEMUSAGE" it's the memory usage of the process (in KB)

"gt" meens greather than

cmd > taskkill /? for more info

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.