Groovy Memory Limiter

Groovy Memory Limiter

On 2018 our team was tasked with the maintenance of a big pile of legacy code esentially handling the execution of user-defined Groovy scripts (think "something like the Jenkins Pipeline").

A big issue occasionally caused by this is an, otherwise well intentioned, user crashing the whole system by running suboptimal code. An example of this would be an script that eats up RAM indefinitely, or an user trying to launch a big DB query and storing the full results directly in memory.

When addressing this became a priority, we turned to the OSS community for a solution (why reinvent the wheel, amirite?) and we found groovy-memory-limiter, the work of Denis Kirpichenkov. It does great limiting memory use!

However we needed some extra features: the possibility of defining custom behaviours when the limit is exceeded, beyond just interrupting the execution, and the ability to get some measure of memory use, as to be able to keep track of what's causing system load at any particular time.

Thus, we took it upon ourselves to implement such features and the result is com.corunet:groovy-memory-limiter, a little open source library that can:

  • Limit memory usage of Groovy scripts
  • Execute custom code (log, warn users by email, you name it...) when such a limit is reached
  • Give you stats of memory usage (maximum registered, average memory)

You can find it on Corunet's GitHub and Maven Central.