Killing the process with a lot of memory



  • Is there a sophth that will monitor applications that are rapidly beginning to consume a lot of HEU and kill them? I'm afraid that when you're listening to music in the vault, or when the OS websites open, it depends on the mortuary of Opera if you don't turn off the deposit in time, but it's not easy to notice. The memtestant didn't run until, because if I bought new plans, a couple weeks later. The problem brings a lot of inconvenience.

    By the way, vm.swappiness is zero. It's 10 now, maybe the problem is gone.



  • That's the dumbest approach. We're not going to go into the details: as correct as it is, we're not gonna break the system, and so on.

    #!/bin/sh
    

    while [ 1 ]
    do

    получаем %памяти, имя процесса, его PID и юзера:

    ps axo %mem,comm,pid,euser | \

    если процесс занимает больше 10% - убиваем:

    awk -F' ' '{
    if( $1 > 10 ) { \
    printf( "KILL %s:%s - %s\n",$2,$4,$3 );
    system( "kill -9 " $3 );
    }
    }'

    sleep 10

    done

    We can go further, first we can determine how many memories are free, and start murders only if there is less than a limit. Introduce a list of non-threatening exceptions, calculate not per cent of the total, but from work/free memory, etc. For example:

    #!/usr/bin/perl

    use Modern::Perl;

    свободной памяти больше - не дёргаемся:

    use constant MIN_FREE_MEM => 40;

    сколько %% памяти разрешается занимать процессу:

    use constant MAX_PROCESS_MEM => 10;
    use constant SLEEP_DELAY => 10;

    будем использовать смотреть точное совпадение,

    но можно и под регекспы переделать

    my %excludes = (
    'init' => 1,
    'eclipse' => 1,
    );

    while (1) {

    элементы массива будут такими:

    0 1 2 3

    [ %памяти, имя процесса, его PID, юзер ]

    my @pinfo = map {
    s/^\s+|\s+$//g;
    [ split /\s+/ ];
    } split "\n", ps axo %mem,comm,pid,euser;

    удалить первую строку с заголовком

    shift @pinfo;

    my $free = 100.0;
    $free -= $_->[0] for @pinfo;

    теперь мы знаем сколько %% памяти свободно

    if ( $free < MIN_FREE_MEM ) {
    # пропускаем исключения:
    next if $excludes{ $->[1] };
    for (@pinfo) {
    if ( $
    ->[0] > MAX_PROCESS_MEM ) {
    say "Killing process $->[2]:$->[1]:$->[3] ($->[0]%)";
    kill '-KILL', $_->[2];
    }
    }
    }
    sleep SLEEP_DELAY;
    }


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2