# Logging memory usage in the builds

**URL:** <https://discuss.circleci.com/t/logging-memory-usage-in-the-builds/442>\
**Category:** Build Environment\
**Tags:** oom, memory-usage\
**Created:** [November 10, 2015, 12:21pm UTC](https://discuss.circleci.com/t/logging-memory-usage-in-the-builds/442 "2015-11-10T12:21:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexey](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/alexey/32/42_2.png) [@alexey](https://discuss.circleci.com/u/alexey)\
**Post date:** [November 10, 2015, 12:21pm UTC](https://discuss.circleci.com/t/logging-memory-usage-in-the-builds/442/1 "2015-11-10T12:21:37Z")

</div>

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](https://circleci.com/docs/oom/). 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.

---

<div class="post-metadata">

**Author:** ![einfallstoll](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/einfallstoll/32/299_2.png) [@einfallstoll](https://discuss.circleci.com/u/einfallstoll)\
**Post date:** [November 10, 2015, 1:07pm UTC](https://discuss.circleci.com/t/logging-memory-usage-in-the-builds/442/2 "2015-11-10T13:07:02Z")

</div>

> [@alexey](#):
>
> […] into the Artifacts tab for that build.

A deeper insight to Artifacts can be found on the [blog](https://circleci.com/blog/build-artifacts/).

---

<div class="post-metadata">

**Author:** ![TTransmit](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/ttransmit/32/12301_2.png) [@TTransmit](https://discuss.circleci.com/u/TTransmit)\
**Post date:** [November 16, 2016, 10:36pm UTC](https://discuss.circleci.com/t/logging-memory-usage-in-the-builds/442/3 "2016-11-16T22:36:30Z")

</div>

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

```auto
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:

```auto
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

---

<div class="post-metadata">

**Author:** ![einfallstoll](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/einfallstoll/32/299_2.png) [@einfallstoll](https://discuss.circleci.com/u/einfallstoll)\
**Post date:** [November 17, 2016, 8:33am UTC](https://discuss.circleci.com/t/logging-memory-usage-in-the-builds/442/4 "2016-11-17T08:33:02Z")

</div>

`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](https://circleci.com/docs/configuration/#artifacts))

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](https://circleci.com/docs/background-process/)

---

<div class="post-metadata">

**Author:** ![TTransmit](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/ttransmit/32/12301_2.png) [@TTransmit](https://discuss.circleci.com/u/TTransmit)\
**Post date:** [November 17, 2016, 12:43pm UTC](https://discuss.circleci.com/t/logging-memory-usage-in-the-builds/442/5 "2016-11-17T12:43:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/circleci/original/3X/5/1/511608acda8e85906e15909b57bd0a46c1dbe291.png) [@system](https://discuss.circleci.com/u/system)\
**Post date:** [June 18, 2018, 11:37am UTC](https://discuss.circleci.com/t/logging-memory-usage-in-the-builds/442/6 "2018-06-18T11:37:35Z")

</div>


