Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking for a way to execute a script as my RAM fills up, right before my machine starts swapping heavily and blocks my mouse, keyboard and other input devices for some minutes.

The thing is that I'm developing software, and it's pretty hefty in terms of memory use. Usually its my fault if it does fill up my RAM, but I need a way to auto-kill the app I'm developing when this happens. (Or preferably right before)

Can I receive events from the OS itself? Or from maybe the system monitor? I need some lightweight, and preferably non-polling way of doing this.

I'm running Ubuntu 11.04 btw.

share|improve this question
Why not turn off swap so when your program runs out of memory you'll get the appropriate error, and then you can take the appropriate action? Most PCs these days have enough RAM that this should not be an issue. And what's the use of swap space if it cripples your machine? – Mike Nov 7 '11 at 18:12
@Mike Swap is not only used when RAM is full, it is used for various memory housekeeping activities. Disabling swap is not good practice. – Paul Nov 8 '11 at 5:41
@Paul, I know that, but let's think outside of the box here. If he's debugging an application that's so aggressive about allocating memory that if it allocates too much memory everything else swaps out, that's a problem. I wonder what his ratio of swap space to physical memory is? Cutting swap out of the equation (or trimming it down to size), I would argue, would help the OP's software development efforts because he would see his bug faster, before the critical point that he can't do anything with his machine any more. – Mike Nov 8 '11 at 6:01
Sure - my concern was that this site is a resource as much as an answer forum. This comment "Most PCs these days have enough RAM that this should not be an issue. And what's the use of swap space if it cripples your machine?" could lead novices to turn off swap thinking it was itself a problem. – Paul Nov 8 '11 at 6:03
For the record, ulimit is a better solution than disabling swap. ;-) But potentially just as dangerous if wielded incorrectly. (also, I work on embedded systems, so the notion of having swap space available at all in my development environment is a bit laughable) ;-) – Mike Nov 8 '11 at 6:05
up vote 1 down vote accepted

You could set ulimits before running the program, or from inside the program with setrlimit.

See ulimit -a from bash and man setrlimit

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .