It is often useful to impose memory limits on long-running programs to reduce the impact of memory leaks or security problems. However, it can be quite difficult to estimate what limit should be used, particularly when memory usage can vary wildly depending on load.
A brute-force approach would be a script that ran a command-line repeatedly, doing a binary-search to find memory limits that don't cause it to fail. The command-line should be set to run the desired program with a representative load.