Logging memory usage in the builds

If the build hits the memory limit, CircleCI will save a file called memory-usage.txt into the Artifacts tab for that build. This is described in more detail in this doc. However, the memory usage will only be logged in the builds that finish with out-of-memory errors.

If you would like to monitor memory usage in your build pro-actively, you could add something like this to your circle.yml file:

- FILE=$CIRCLE_ARTIFACTS/memory-usage-actual.txt; while true; do ps -u ubuntu eo pid,%cpu,%mem,args,uname --sort=-%mem >> $FILE; echo "----------" >> $FILE; sleep 1; done: # note the colon here
    background: true

This command will run in the background and will generate the memory usage stats every second till the build finishes, one per line.

2 Likes

A deeper insight to Artifacts can be found on the blog.

2 Likes

We tried the code above by adding it to the end of an existing circle.yml file:

general:
  artifacts:
    - FILE=$CIRCLE_ARTIFACTS/memory-usage-actual.txt; while true; do ps -u ubuntu eo pid,%cpu,%mem,args,uname --sort=-%mem >> $FILE; echo "----------" >> $FILE; sleep 1; done:
        background: true

It gave the following error message:

There was an error while parsing your circle.yml: artifacts must be a list of strings

Action failed: Configure the build

It is the background: true setting that appears to cause the error.

Any suggestions? Thanks

artifacts should be as stated in the error message an array of files and directories. The paths listed there will end up in the artifacts tab in your build. (See Specifying custom artifacts directories and files)

However if you put any file within the directory that $CIRCLE_ARTIFACTS is pointing to they’ll automatically show up in your build artifacts as well. Therefore the artifacts array is completely obsolete for this purpose, as the script will put the stdout at the right place.

So far this is the background of the error you’re facing, but not yet the answer to your question. What you try to achieve is running a background process that stores it’s output in the given artifacts directory. To run a background process you’ll have to specify the command, as well as the background flag, in the post array:

machine:
  post:
    - FILE=$CIRCLE_ARTIFACTS/memory-usage-actual.txt; while true; do ps -u ubuntu eo pid,%cpu,%mem,args,uname --sort=-%mem >> $FILE; echo "----------" >> $FILE; sleep 1; done:
        background: true

For more information see Start background processes from circle.yml

1 Like

Thanks einfallstoll. This has worked and is letting us see memory usage over time for builds.