Anirvan
Anirvan

Reputation: 6364

How to limit process memory utilization on Linux (e.g. using BSD::Resource)

I'd like to limit the memory usage for my Perl script, running on a Linux system. I've been trying to use BSD::Resource's setrlimit, but have been having problems. I'd appreciate any pointers. Thank you.

Upvotes: 7

Views: 3532

Answers (2)

timkay
timkay

Reputation: 727

When you are developing code, it's easy to have your Perl program run away and consume all of memory. The machine will grind to a halt, until the program exhausts memory and dies. You can prevent this problem:

Use this code:

use BSD::Resource;
setrlimit(get_rlimits()->{RLIMIT_VMEM}, 1_000_000_000, -1) or die;
1;

I put this code in limit.pm (hence the "1;"). I can then say

use limit;

at the top of any program that I want to limit.

Upvotes: 8

Anders Lindahl
Anders Lindahl

Reputation: 42910

Scott Corely suggests setting ulimit before running the perl script.

Upvotes: 3

Related Questions