top -b -n 1 -o %MEM > toptest.txt

This is a super helpful command that runs the top command one time in batch mode sorted by the memory used by each process and saves the output to a file names toptest.txt. Note that by using “>” a new file is created each time and any old files with the same name as toptest.txt is overwritten. You can append to existing files by using “>>” instead of “>”.