1

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.

5
  • 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?
    – mpontillo
    Nov 7, 2011 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, 2011 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.
    – mpontillo
    Nov 8, 2011 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, 2011 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) ;-)
    – mpontillo
    Nov 8, 2011 at 6:05

1 Answer 1

1

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

See ulimit -a from bash and man setrlimit

You must log in to answer this question.

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