How to Check Thread Count and Memory Usage per Process in Linux (Ubuntu 9.10 Server)


3 views

On Linux systems, there are several ways to check the number of threads for each running process. Here are the most practical methods:


# Method 1: Using ps command
ps -eLf | awk '{print $2,$5}' | sort -n | uniq -c | sort -n

# Method 2: Using htop (requires installation)
sudo apt-get install htop
htop

# Method 3: Checking /proc directory
ls /proc/[0-9]*/task/ | wc -l

To examine memory consumption per process, these commands prove most effective:


# Basic memory overview
top -o %MEM

# Detailed RSS memory usage
ps -eo pid,user,pcpu,pmem,rss,vsz,comm --sort=-rss | head -20

# Per-process detailed breakdown (replace PID)
cat /proc/[PID]/status | grep -i vm

For comprehensive monitoring, consider this Python script that combines both metrics:


#!/usr/bin/env python
import os, glob

def get_process_info():
    for pid in [d for d in glob.glob('/proc/[0-9]*')]:
        try:
            with open(f"{pid}/status") as f:
                status = f.read()
            name = [l.split(':')[1].strip() for l in status.split('\n') if 'Name:' in l][0]
            threads = [l.split(':')[1].strip() for l in status.split('\n') if 'Threads:' in l][0]
            rss = [l.split(':')[1].strip() for l in status.split('\n') if 'VmRSS:' in l]
            rss = rss[0].split()[0] if rss else '0'
            print(f"PID: {pid.split('/')[-1]}, Name: {name}, Threads: {threads}, RSS(kB): {rss}")
        except:
            continue

get_process_info()

For long-term monitoring, consider these approaches:

  • Implement a cron job that logs thread counts and memory usage hourly
  • Use atop for advanced historical process monitoring
  • Configure collectd or telegraf with process monitoring plugins

When encountering excessive threads:


# Find processes with most threads
ps -eLf | awk '{print $2,$5}' | sort -n | uniq -c | sort -nr | head

# Analyze specific process thread stacks
pstack [PID] | more

To check the number of threads per process in Ubuntu 9.10, these are the most effective methods:

# Method 1: Using ps with custom format
ps -eLf | awk '{print $2,$5}' | sort -n | uniq -c | sort -n

# Method 2: Parsing /proc directory
ls /proc/[0-9]*/task/ | xargs -I {} sh -c 'echo -n "PID {}: "; ls /proc/{}/task/ | wc -l'

For interactive monitoring, install htop (if not available in Ubuntu 9.10, you'll need to compile from source):

sudo apt-get install htop
htop

Press F2 to enter setup, navigate to Display Options, and enable "Tree view" and "Show custom thread names" for better visualization.

To get precise memory metrics per process, use these approaches:

# RSS (Resident Set Size) measurement
ps -eo pid,user,comm,rss --sort -rss | head -n 10

# Detailed memory breakdown (requires smem)
sudo apt-get install smem
smem -s rss -r -k -c "pid user command rss pss"

For comprehensive process analysis, this Python script combines both metrics:

#!/usr/bin/env python
import os, glob

def get_process_info():
    for pid_dir in glob.glob('/proc/[0-9]*'):
        try:
            pid = os.path.basename(pid_dir)
            with open(f'/proc/{pid}/status') as f:
                status = dict(line.split(':\t') for line in f)
            
            threads = len(glob.glob(f'/proc/{pid}/task/*'))
            rss_kb = int(status['VmRSS'].split()[0]) if 'VmRSS' in status else 0
            
            print(f"PID {pid}: {threads} threads | {rss_kb} KB RSS | {status['Name']}")
        except:
            continue

if __name__ == '__main__':
    get_process_info()

For Ubuntu 9.10 specifically, some modern tools may not be available. Here are alternative approaches:

# Old-school thread count
pstree -p | grep -o '([0-9]*)' | wc -l

# Memory via procfs
cat /proc/$(pidof your_process)/status | grep -E 'VmSize|VmRSS|Threads'

Remember that Ubuntu 9.10 reached end-of-life in April 2011, so consider upgrading for security and compatibility reasons.