Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

memory

perl

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.

like image 206
Anirvan Avatar asked Jun 09 '09 07:06

Anirvan


People also ask

How is memory utilization percentage calculated in Linux?

Keeping in mind the formula, MEM%= 100-(((free+buffers+cached)*100)/TotalMemory).

What is VmPeak in Linux?

You can get the peak memory usage of a certain process, at: grep VmPeak /proc/$PID/status. (Change $PID to the actual process id you're looking for). VmPeak is the maximum amount of memory the process has used since it was started.


2 Answers

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.

like image 125
timkay Avatar answered Sep 22 '22 23:09

timkay


Scott Corely suggests setting ulimit before running the perl script.

like image 25
Anders Lindahl Avatar answered Sep 20 '22 23:09

Anders Lindahl