Sungju's Slow Life

Personal journal


How to find the actual memory usage by each process

Checking RSS gives you an idea how much physical memory is consumed at the moment. However, there are shared area such as shared object (.so), shared memory (shmget), etc.

To get real amount consumed by application side, those shared area should be divided by the number of processes which is done by kernel in /proc/*/smaps file using Pass: field.

Here I made a simple alias to check the difference.

# RSS related functions
alias rss_task='function __rss_task() { \
	local taskid=$1; \
	grep "^Rss:" /proc/$taskid/smaps | \
	awk "BEGIN { sum=0 } { sum=sum+\$2 } END { printf \"pid: $taskid, pages: %d\n\", sum }"; \
}; \
__rss_task'

alias rss_all_task='function __rss_all_task() { \
	cd /proc; \
	for taskid in [0-9]*; do \
		rss_task $taskid; \
	done; \
}; \
__rss_all_task'

alias total_rss='function __total_rss() { \
	rss_all_task | \
	awk "BEGIN { sum=0 } { sum=sum+\$4 } END { printf \"Total RSS: %d pages, %d KB\n\", sum, sum * 4 }"; \
}; \
__total_rss'

# PSS related functions
alias pss_task='function __pss_task() { \
	local taskid=$1; \
	grep "^Pss:" /proc/$taskid/smaps | \
	awk "BEGIN { sum=0 } { sum=sum+\$2 } END { printf \"pid: $taskid, pages: %d\n\", sum }"; \
}; \
__pss_task'

alias pss_all_task='function __pss_all_task() { \
	cd /proc; \
	for taskid in [0-9]*; do \
		pss_task $taskid; \
	done; \
}; \
__pss_all_task'

alias total_pss='function __total_pss() { \
	pss_all_task | \
	awk "BEGIN { sum=0 } { sum=sum+\$4 } END { printf \"Total PSS: %d pages, %d KB\n\", sum, sum * 4 }"; \
}; \
__total_pss'

Here I made six aliases – rss_task, rss_all_task, total_rss, pss_task, pss_all_task, total_pss. Using those aliases, I can get memory related values. Below is the result from my test machine which shows almost 1GB (1227000KB) difference.

$ total_rss
Total RSS: 929600 pages, 3718400 KB

$ total_pss
Total PSS: 622850 pages, 2491400 KB

To find out the top active consumers, you could use sort or similar utilities.

$ rss_all_task | sort -n -k 4 | tail -n 4
pid: 2034, pages: 67944
pid: 2392, pages: 68496
pid: 2495, pages: 75844
pid: 1562, pages: 279092

$  pss_all_task | sort -n -k 4 | tail -n 4
pid: 2034, pages: 61998
pid: 2392, pages: 62554
pid: 2495, pages: 70633
pid: 1562, pages: 271688

It shows that the top consumer is pid 1562 which was consumed about 265MB.



Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: