Search code examples
haskellgarbage-collection

Run the GC based on physical memory


I am running my code on AWS Lambda and I have some performance issues depending on the memory I allocate to the Lambda.

For the same call:

             128 MB | 256 MB
           +--------+-------+
Cold start |  3.5s  |   2s  |
-----------+--------+-------+
Nominal    | 0.14s  | 0.14s |

The maximum used memory is always 85-90 MB.

My guess is that the GC is triggered too often.

I have tried to look the RTS doc, it looks like -c <n> would allow to increase the threshold but it requires me to use -M <size> but I do not know what to put in there.

Is there a way to base a threshold on the physical memory or the total memory?


Solution

  • I guess one way would be via shell substitution. Then you can put whatever calculation you want in there. For example, on my machine, something like this computes half the available memory:

    (echo scale=0; sed '/^MemTotal:/!d;s/[^0-9]//g;s.$./2.' /proc/meminfo) | bc
    

    Explanation: extract the MemTotal: ... line from /proc/meminfo, delete everything that isn't the number, and append /2 to divide by 2. The initial scale=0 tells bc to round to the nearest whole number.

    Rolling that into your command's argument list then looks like this:

    myFancyHaskellProgram +RTS -M $( (echo scale=0; sed '/^MemTotal:/!d;s/[^0-9]//g;s.$./2.' /proc/meminfo) | bc )K