<jupyter_start><jupyter_text>How To Check Memory Usage Per Process On Linux?<jupyter_code>%%bash

PID=2489 ##This is Processid
awk '/^Pss:/{ sum += $2 } END {printf "%.1f MB\n", sum/1024}' \
/proc/$PID/smaps

from pathlib import Path
PID='2489'
file = Path('/proc/'+PID+'/smaps').read_text()
Pss = [int(i.strip().split()[1]) for i in file.splitlines()
if i.startswith("Pss")]
print(round(sum(Pss)/1024, 1), 'MB')<jupyter_output>42.7 MB<jupyter_text>Calculate Private, Shared Memory Per Process<jupyter_code>%%bash

PID=2489

Pss=$(awk '/^Pss:/{ sum += $2 } END {print sum/1024}' /proc/$PID/smaps)
Private=$(awk '/^Private/{ sum += $2 } END {print sum/1024}' /proc/$PID/smaps)

if [ x"$Rss" != "x" -o x"$Private" != "x" ]; then
Shared=$(awk -v pss=$Pss -v pri=$Private 'BEGIN {print pss - pri}')
Name=$(awk '/^Name/{print $2}' /proc/$PID/status)
Sum=$(awk -v sha=$Shared -v pri=$Private 'BEGIN {print sha + pri}')
echo -e "Private(MB)      Shared(MB)    = Total(MB) \t Process"
printf "%0.s=" {1..57};echo
echo -e "$Private   +      $Shared         = $Sum  \t $Name"
fi
%%python3

from pathlib import Path
import sys

pss = []
private = []

try:
    process = input('Enter Process id: ')    
    osmaps = Path('/proc/'+process+'/smaps').read_text()  
except (ValueError, FileNotFoundError):
    print(sys.exc_info()[1])
    sys.exit(2)
else:
    for text in osmaps.splitlines():
        if text.startswith('Pss'):
            pss.append(int(text.split()[1]))
        if text.startswith('Private'):
            private.append(int(text.split()[1]))

Pss = round(sum(pss)/1024)
Private = round(sum(private)/1024)
Shared = Pss - Private
Total = Private + Shared
print(f'{"Private(MB)"} {"Shared(MB)"} = {"Total(MB)"}')
print(f'{Private:^10} + {Shared:^10} {Total:^10}')<jupyter_output>Enter Process id: 2489
Private(MB) Shared(MB) = Total(MB)
    39     +     4          43
{'fraction_non_alphanumeric': 0.20556745182012848, 'fraction_numerical': 0.03747323340471092, 'mean_word_length': 3.8170103092783507, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '88431', 'license': 'MIT', 'n_tokens_mistral': 761, 'n_tokens_neox': 705, 'n_words': 168, 'path': 'How-To-Check-Memory-Usage-Per-Process.ipynb', 'repo_name': 'ManavalanMicheal/Python-Bash', 'size': '1868'}