Memory: +RTS -s against /usr/bin/time

The sum of the first million integers is 500000500000 and it fits in a register. How much memory the program needs to get there depends entirely on whether the million values exist as data, and that is a choice the programmer makes in both languages. This page makes the choice three ways: a C++ vector that holds the values, a Haskell list that is consumed as it is produced, and a Haskell list that is kept alive.

Two metres are used, one per language, and then both on all three programs. /usr/bin/time -v reports the maximum resident set size of the process, which is what the operating system sees. GHC's +RTS -s reports the maximum residency of the heap, which is what the collector had to keep alive. They answer the same question at two levels and they do not agree, which is the second half of the page.

A vector of a million

The values are put in a std::vector<long>, which is one allocation of eight megabytes, and then summed with std::accumulate. The vector is the job: the program could sum in a loop without it, and the point of the page is what holding the values costs.

#include <cstdio> #include <numeric> #include <vector> constexpr long count = 1000000; int main() { std::vector<long> values; values.reserve(count); for (long i = 1; i <= count; ++i) { values.push_back(i); } const long sum = std::accumulate(values.begin(), values.end(), 0L); std::printf("count %zu sum %ld\n", values.size(), sum); }

g++ -std=c++23 -O2 -Wall -Wextra mem.cpp -o mem
./mem; echo "status $?"

count 1000000 sum 500000500000
status 0

/usr/bin/time -v adds a page of statistics about the process; the two lines worth quoting are the elapsed time and the maximum resident set size, and the command below filters for them so that the page shows exactly what the filter printed:

/usr/bin/time -v ./mem 2>&1 | grep -E "Maximum resident|Elapsed"

    Elapsed (wall clock) time (h:mm:ss or m:ss): 0m 0.01s
    Maximum resident set size (kbytes): 10240
status 0

10,240 kilobytes, of which eight are the vector and the rest is the process, the C library and the page tables. That is the number a monitoring system would report for this program.

A list that is consumed or kept

The Haskell version of the same sum is one line. [1 .. 1000000] is a list, but nothing builds it: the enumeration produces one element at a time, sum consumes it, and the garbage collector is free to reclaim each cons cell as soon as the next one is produced. GHC's optimiser often removes the list entirely.

module Main (main) where import Text.Printf (printf) main :: IO () main = printf "sum %d\n" (sum [1 .. 1000000 :: Int])

+RTS -s prints the runtime statistics after the program, including the maximum residency and the bytes allocated:

ghc -Wall -O1 -o lazy-hs Lazy.hs
./lazy-hs +RTS -s; echo "status $?"

sum 500000500000
          52,864 bytes allocated in the heap
           3,272 bytes copied during GC
          44,328 bytes maximum residency (1 sample(s))
          25,304 bytes maximum slop
               6 MiB total memory in use (0 MiB lost due to fragmentation)

                                     Tot time (elapsed)  Avg pause  Max pause
  Gen  0         0 colls,     0 par    0.000s   0.000s     0.0000s    0.0000s
  Gen  1         1 colls,     0 par    0.000s   0.000s     0.0002s    0.0002s

  INIT    time    0.000s  (  0.000s elapsed)
  MUT     time    0.002s  (  0.002s elapsed)
  GC      time    0.000s  (  0.000s elapsed)
  EXIT    time    0.000s  (  0.000s elapsed)
  Total   time    0.003s  (  0.003s elapsed)

  %GC     time       0.0%  (0.0% elapsed)

  Alloc rate    30,735,455 bytes per MUT second

  Productivity  67.4% of total user, 67.6% of total elapsed

status 0

52,864 bytes allocated for a million values, and a maximum residency of 44,328 bytes, which is the runtime's own baseline and not the data. The same program under /usr/bin/time -v:

/usr/bin/time -v ./lazy-hs 2>&1 | grep -E "Maximum resident|Elapsed"

    Elapsed (wall clock) time (h:mm:ss or m:ss): 0m 0.00s
    Maximum resident set size (kbytes): 2048
status 0

Two megabytes of resident memory, most of which is the GHC runtime rather than the program. The list never existed as a million-element structure, and the two metres agree on that.

The third program keeps the list. It binds values = [1 .. 1000000] and traverses it twice, so the first traversal evaluates the cells and the second finds them already computed and still reachable from values. Nothing can be collected between the two sums:

module Main (main) where import Text.Printf (printf) main :: IO () main = do let values = [1 .. 1000000 :: Int] printf "total %d\n" (sum values + sum (map (* 2) values))

ghc -Wall -O1 -o kept-hs Kept.hs
./kept-hs +RTS -s 2>&1 | grep -E "bytes allocated|maximum residency|total memory"

      64,053,232 bytes allocated in the heap
      26,229,984 bytes maximum residency (4 sample(s))
              62 MiB total memory in use (0 MiB lost due to fragmentation)
status 0

/usr/bin/time -v ./kept-hs 2>&1 | grep -E "Maximum resident|Elapsed"

    Elapsed (wall clock) time (h:mm:ss or m:ss): 0m 0.15s
    Maximum resident set size (kbytes): 64256
status 0

Fifty-two kilobytes became twenty-five megabytes on the GHC heap and sixty-four megabytes of resident memory, for the same answer. The next chapter says where the factor of a thousand comes from.

The numbers on this machine

One machine, the three programs, the two metres. The residency column is GHC's, so the C++ row has none; the resident column is the operating system's and applies to all three.

Program What it holds Max residency (+RTS -s) Max RSS (time -v) Elapsed
mem.cpp a vector of 1,000,000 longs — 10,240 kB 0.01 s
Lazy.hs a list consumed as it is built 44,328 bytes 2,048 kB 0.00 s
Kept.hs a list that stays reachable 26,229,984 bytes 64,256 kB 0.15 s

The machine is the one the container of this site runs on: 4 CPUs (nproc 4), Alpine Linux 3.24.3 on x86_64, g++ 15.2.0 with -std=c++23 -Wall -Wextra -O2, GHC 9.10.3 with -Wall -O1 and the default runtime settings: no +RTS flags except -s, no -threaded, one capability. These are single runs of programs whose peak is deterministic, which is why there is no table of five: the peak does not move between runs the way a time does.

What the two metres measure

A C++ long is eight bytes and a vector of a million of them is eight megabytes, allocated once. The resident set size is that plus the process, and it is what the machine has to find.

A Haskell Int in a list is not eight bytes. The list is a chain of cons cells, each three words: a tag, the head and the tail. The head of this list is a boxed Int, two more words for a pointer and the value. On x86-64 a word is eight bytes, so the naive count is about forty bytes per element. The measured residency of the retained list is 26,229,984 bytes at -O1 and 35,816,872 at -O0: the same order, with the optimiser specialising the enumeration and the two traversals and keeping less per element, and not removing the structure.

The resident set is larger than the residency because the runtime maps its heap in blocks and does not return it all to the operating system: 26 megabytes of live data in a runtime that reports 62 MiB in use and 64 megabytes of resident memory is the same fact seen from two sides. +RTS -s is the metre for a Haskell program because it measures what the collector keeps; time -v is the metre for a deployment because it measures what the machine gives.

The comparison that matters for a design is not 10 against 64 megabytes. It is the first Haskell program against the third: the same source, one binding apart, and a factor of a thousand in memory. That is the price of laziness when a value is retained, and it is the subject of the course lesson on space leaks and +RTS -s.

The list that stays alive

The third program is the mistake written on purpose, and it is a mistake a reader makes in good faith: bind the data to a name because two functions need it, and the name keeps it alive. A C++ programmer would say that the vector is held by the variable; the difference is that the C++ programmer sees the eight megabytes in the type and the Haskell programmer sees [Int], which looks like a number.

The fix has the same shape in both languages: do not keep what you do not need. In Haskell that means one traversal instead of two, or a fold that accumulates both results at once, or a strict field that forces the value before the list is built. The benchmark lesson is the one from the previous page: a number without the representation behind it is a number about something else.

The Haskell lessons behind this page are laziness and infinite lists and seq, $! and bang patterns, and the next page in this set is One change made in both.

The C++ programs were built with g++ 15.2.0 (Alpine 15.2.0) using -std=c++23 -Wall -Wextra -O2 and the Haskell programs with GHC 9.10.3 using -Wall -O1, and measured with the /usr/bin/time of GNU time and the +RTS -s of GHC 9.10.3 on a 4-CPU Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image