Script to Get User That Has Process with Most Memory Usage

Find out which processes use the most memory and kill them

As @chepner suggested, I'm now monitoring memory through my program thanks to a Java socket which will receive a signal if memory in to high and then will nicely shutdown the process.

Get-Process with total memory usage

Format-Table can show expressions and auto-size the columns to fit the results:

On 64 bits:

get-process -computername $tag1 | Group-Object -Property ProcessName | 
Format-Table Name, @{n='Mem (KB)';e={'{0:N0}' -f (($_.Group|Measure-Object WorkingSet64 -Sum).Sum / 1KB)};a='right'} -AutoSize

On 32 bits:

get-process -computername $tag1 | Group-Object -Property ProcessName | 
Format-Table Name, @{n='Mem (KB)';e={'{0:N0}' -f (($_.Group|Measure-Object WorkingSet -Sum).Sum / 1KB)};a='right'} -AutoSize

Python Script to Return Process with highest Memory Usage

import psutil

pids = psutil.pids()
processes = map(psutil.Process, pids)
most_mem_process = max(processes, key=lambda p: p.memory_full_info().data)

How to display processes that are using memory in an given range

This will list processes as expected. Remember that ps shows memory size in kilobytes.

ps -u 1000 -o pid,user,stime,rss \
| awk '{if($4 > 50000 && $4 < 100000){ print $0 }}' \
| sort -n -k 4,4

Command output:

 3407 luis.mu+ 10:30 51824
3523 luis.mu+ 10:30 66108
3410 luis.mu+ 10:30 71060
3595 luis.mu+ 10:30 74340
3609 luis.mu+ 10:30 77772
18550 luis.mu+ 16:47 93616

In that case it's showing only 4 fields for user id 1000. To show all processes use

ps -e -o pid,user,stime,rss

From the ps(3) man page under STANDARD FORMAT SPECIFIERS:

rss

resident set size, the non-swapped physical memory that a task has used (inkiloBytes)

If you want to show more fields, check the man page and add fields to -o option.

How to measure total memory usage of all processes of a user in Python

It's pretty simple using psutil. You can just iterate over all processes, select the ones that are owned by you and sum the memory returned by memory_info().

import psutil
import getpass

user = getpass.getuser()

total = sum(p.memory_info()[0] for p in psutil.process_iter()
if p.username() == user)

print('Total memory usage in bytes: ', total)

Checking for total memory usage in a bash script which spawns other processes

If child processes don‘t have their own child it’s quite easy:

$ ps h --ppid "$PID" -o vsz | awk '{ SUM+=$0 }; END { print SUM }'

Otherwise you may to rely on process group ID (PGID), but it’s worth to ensure that programs you call from script do not set up their’s own process groups.

$ ps h -e -o pgid,vsz | awk -v "PGID=$PID" '$1==PGID { SUM+=$2 }; END { print SUM }'

If all children of your script do not belong to the same PGID, we can only loop through processes recursively:

$ cat ~/bin/vsztree

#!/bin/bash

declare -i VSZ_TOTAL

vsz() {
while read PID VSZ; do
VSZ_TOTAL+=$VSZ
vsz "$PID"
done < <(ps --ppid "$1" --format pid,vsz --no-headers)
}

vsz "$1"
echo $VSZ_TOTAL

$ vsztree "$PID"

Here $PID is a PID of your script. Output is a total virtual memory size in kilobytes.

Script to identify Process Name , Memory , Account Owner in Windows server

There's may be a cleaner way to do this, but here's an example:

$users = @{}
$process = Get-Process

Get-WmiObject Win32_SessionProcess | ForEach-Object {

$userid = (($_.Antecedent -split “=”)[-1] -replace '"' -replace “}”,“”).Trim()
if($users.ContainsKey($userid))
{
#Get username from cache
$username = $users[$userid]
}
else
{
$username = (Get-WmiObject -Query "ASSOCIATORS OF {Win32_LogonSession.LogonId='$userid'} WHERE ResultClass=Win32_UserAccount").Name
#Cache username
$users[$userid] = $username
}

$procid = (($_.Dependent -split “=”)[-1] -replace '"' -replace “}”,“”).Trim()
$proc = $process | Where-Object { $_.Id -eq $procid }

New-Object psobject -Property @{
UserName = $username
ProcessName = $proc.Name
"WorkingSet(MB)" = $proc.WorkingSet / 1MB
}
}

OUTPUT:

UserName ProcessName       WorkingSet(MB)
-------- ----------- --------------
Frode taskhostex 61,5
Frode explorer 172,33203125
Frode RuntimeBroker 21,9375
Frode HsMgr 5,578125
Frode HsMgr64 5,453125
Frode SetPoint 17,4375

The code needs to run as admin to get sessions for other users(not just the current user).



Related Topics



Leave a reply



Submit