Unix command to tell how much RAM was used during program runtime?

The time(1) command (you may need to install it -perhaps as the time package-, it should be in /usr/bin/time) accepts many arguments, including a format string (with -f or --format) which understands (among others)

  %M     Maximum resident set size of the process during its lifetime,
          in Kbytes.

  %K     Average total (data+stack+text) memory use of the process, in
          Kbytes.

Don't confuse the /usr/bin/time command with the time bash builtin. You may need to type the full file path /usr/bin/time (to ask your shell to run the command not the builtin) or type command time or \time (thanks to Toby Speight & to Arrow for their comments).

So you might try (RSS being the resident set size)

/usr/bin/time -f "mem=%K RSS=%M elapsed=%E cpu.sys=%S .user=%U" python script1.py

You could also try

/usr/bin/time --verbose  python script1.py

You are asking:

how much RAM was used as the script was running?

and this shows a misconception from your part. Application programs running on Linux (or any modern multi-process operating system) are using virtual memory, and each process (including the python process running your script) has its own virtual address space. A process don't run directly in physical RAM, but has its own virtual address space (and runs in it), and the kernel implements virtual memory by sophisticated demand-paging using lazy copy-on-write techniques and configures the MMU. The RAM is a physical device and resource used -and managed internally by the kernel- to implement virtual memory (read also about the page cache and about thrashing).

You may want to spend several days understanding more about operating systems. I recommend reading Operating Systems : Three Easy Pieces which is a freely downloadable book. The RAM is used by the entire operating system (not -directly- by individual processes) and the actual pages in RAM for a given process can vary during time (and could be somehow shared with other processes). Hence the RAM consumption of a given process is not well defined since it is constantly changing (you may want its average, or its peak value, etc...), and likewise for the size of its virtual address space.

You could also use (especially if your script runs for several seconds) the top(1) utility (perhaps in some other terminal), or ps(1) or pmap(1) -maybe using watch(1) to repeat that ps or pmap command. You could even use directly /proc/ (see proc(5)...) perhaps as watch cat /proc/$(pidof python)/status or /proc/$(pidof python)/stat or /proc/$(pidof python)/maps etc...

But RAM usage (by the kernel for some process) is widely varying with time for a given process (and even its virtual address space is changing, e.g. by calls to mmap(2) and munmap used by ld-linux(8), dlopen(3), malloc(3) & free and many other functions needed to your Python interpreter...).

You could also use strace(1) to understand the system calls done by Python for your script (so you would understand how it uses mmap & munmap and other syscalls(2)). You might restrict strace with -e trace=%memory or -e trace=memory to get only memory (i.e. virtual address space) related system calls.

BTW, the tracemalloc Python feature could be also useful.

I guess that you only care about virtual memory, that is about virtual address space (but not about RAM), used by the Python interpreter to run your Python script. And that is changing during execution of the process. The RSS (or the maximal peak size of the virtual address space) could actually be more useful to know.

See also LinuxAteMyRAM.


You can also use the legendary valgrind, although you may need to install it from your package manager.

$ valgrind c-projects/objtest/bin/objtest 
==6543== Memcheck, a memory error detector
==6543== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==6543== Using Valgrind-3.12.0.SVN and LibVEX; rerun with -h for copyright info
==6543== Command: c-projects/objtest/bin/objtest
==6543== 
|ERROR|array:189: array_delete: delete index 0 but the highest is -1 (delete from empty array): index out of bounds
==6543== 
==6543== HEAP SUMMARY:
==6543==     in use at exit: 480 bytes in 20 blocks
==6543==   total heap usage: 7,390 allocs, 7,370 frees, 256,217 bytes allocated
==6543== 
==6543== LEAK SUMMARY:
==6543==    definitely lost: 96 bytes in 4 blocks
==6543==    indirectly lost: 384 bytes in 16 blocks
==6543==      possibly lost: 0 bytes in 0 blocks
==6543==    still reachable: 0 bytes in 0 blocks
==6543==         suppressed: 0 bytes in 0 blocks
==6543== Rerun with --leak-check=full to see details of leaked memory
==6543== 
==6543== For counts of detected and suppressed errors, rerun with: -v
==6543== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

If you are a power user and want fancy graphs, you can use ms_print and a command like:

valgrind --tool=massif --pages-as-heap=yes --massif-out-file=massif.out ./test.sh; grep mem_heap_B massif.out | sed -e 's/mem_heap_B=\(.*\)/\1/' | sort -g | tail -n 1

see Peak memory usage of a linux/unix process.


You can use pmap command to view the amount of memory used by a process. In your case you need to give the PID of the script as input to the pmap command like

pmap $(ps -ef | grep **<SCRIPT NAME>** | grep -v grep | awk '{print $2}')