Killing Memory Leaks In FreeBSD

metadata

I run a cron job on one of my FreeBSD machines that runs a Perl script every night. Unfortunately, there appears to be a memory leak somewhere in the script (or within Perl itself) and if the script needs to run for a long time then it can often force FreeBSD to start swapping before FreeBSD eventually kills it because my machine has run out of RAM.

This post is simply to highlight a nice feature that exists on FreeBSD: limits. It is a simple program that allows you to run scripts and limit the resource that they use. For example, the following command will work fine: limits -v 1g python3 -c 'import numpy; foo = numpy.zeros(128 * 1024 * 512, dtype = numpy.float64); print(foo[0]); import time; time.sleep(10.0)' and will use 512 MiB of RAM. If you double the size of the array then the process will crash out instantly when it reaches the specified 1 GiB limit - way before any swapping occurs.