This question is covered here in great detail.
How do you measure the memory usage of an application or process in Linux?
From the blog articl
Check out this shell script to check memory usage by application in Linux.
It is also available on GitHub and in a version without paste and bc.