1

The time command is very useful for checking how much time it takes for a piece of code that I develop to run... However, I'd like to have a way to check the memory consumption of the codes that I develop too, regardless of the language that I use. So, if it's bash, python, or node.js... I'd like to have a way of checking how much RAM memory I used on the code, just so I can get more aware of what I'm doing avoiding memory duplication and stuff like that. Is there any command line that I can use for checking the amount of memory that a script that I create consumes?

raylight
  • 461
  • 1
  • 6
  • 15

1 Answers1

1

On many Unix-like systems, yes, GNU’s implementation of /usr/bin/time (with the path, to avoid the similar shell built-in) will tell you how much memory a given program execution used; for example:

$ /usr/bin/time ls
...
0.00user 0.00system 0:00.00elapsed 50%CPU (0avgtext+0avgdata 2208maxresident)k
0inputs+0outputs (0major+139minor)pagefaults 0swaps

shows that ls used at most 2208K of RAM.

Other tools such as Valgrind will show more information, specifically concerning heap usage:

$ valgrind ls
...
==10107== 
==10107== HEAP SUMMARY:
==10107==     in use at exit: 32,928 bytes in 83 blocks
==10107==   total heap usage: 506 allocs, 423 frees, 97,271 bytes allocated
==10107== 
==10107== LEAK SUMMARY:
==10107==    definitely lost: 0 bytes in 0 blocks
==10107==    indirectly lost: 0 bytes in 0 blocks
==10107==      possibly lost: 0 bytes in 0 blocks
==10107==    still reachable: 32,928 bytes in 83 blocks
==10107==         suppressed: 0 bytes in 0 blocks
==10107== Rerun with --leak-check=full to see details of leaked memory
==10107== 
==10107== For counts of detected and suppressed errors, rerun with: -v
==10107== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Stephen Kitt
  • 434,908