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
--format) which understands (among others)
%M Maximum resident set size of the process during its lifetime,
%K Average total (data+stack+text) memory use of the process, in
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
pmap command. You could even use directly
/proc/ (see proc(5)...)
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
munmap and other syscalls(2)). You might restrict
-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.